我有一个文本文件,包含我需要摆脱的换行符。它有多个块,每个块之间有一个空行。它的格式如下:
A7233334 PALLETS 2 11.000 EACH 2331 Storm Supply 247.5000 2,722.50 2,722.50 4144 Gentro Sales 225.0000 2,475.00 2,475.00 5155 Wacca Outfit 225.0000 2,475.00 2,475.00 6661 Acme Warehouse & Fuller Supply 225.0000 2,475.00 2,475.00 1661 McHugh & Donaldson 225.0000 2,475.00 2,475.00 2344 K-10 & Dunn Co 225.0000 2,475.00 2,475.00 6662 Ronaldson and Son Co. 225.0000 2,475.00 2,475.00
A7233335 CRATES 2 1.000 EACH 2331 Storm Supply 147.5000 1,722.50 1,722.50 4144 Gentro Sales 125.0000 1,475.00 1,475.00 5155 Wacca Outfit 125.0000 1,475.00 1,475.00 6661 Acme Warehouse & Fuller Supply 125.0000 1,475.00 1,475.00 1661 McHugh & Donaldson 125.0000 1,475.00 1,475.00 2344 K-10 & Dunn Co 125.0000 1,475.00 1,475.00 6662 Ronaldson and Son Co. 125.0000 1,475.00 1,475.00
我需要用换行符拆开行,以便输出到这样的新文件:
A7233334 PALLETS 2 11.000 EACH 2331 Storm Supply 247.5000 2,722.50 2,722.50 4144 Gentro Sales 225.0000 2,475.00 2,475.00 5155 Wacca Outfit 225.0000 2,475.00 2,475.00 6661 Acme Warehouse & Fuller Supply 225.0000 2,475.00 2,475.00 1661 McHugh & Donaldson 225.0000 2,475.00 2,475.00 2344 K-10 & Dunn Co 225.0000 2,475.00 2,475.00 6662 Ronaldson and Son Co. 225.0000 2,475.00 2,475.00
A7233335 CRATES 2 1.000 EACH 2331 Storm Supply 147.5000 1,722.50 1,722.50 4144 Gentro Sales 125.0000 1,475.00 1,475.00 5155 Wacca Outfit 125.0000 1,475.00 1,475.00 6661 Acme Warehouse & Fuller Supply 125.0000 1,475.00 1,475.00 1661 McHugh & Donaldson 125.0000 1,475.00 1,475.00 2344 K-10 & Dunn Co 125.0000 1,475.00 1,475.00 6662 Ronaldson and Son Co. 125.0000 1,475.00 1,475.00
有什么想法吗?
答案 0 :(得分:1)
这个awk one liner可以做到:
$ awk '{if ($0 ~ /&$/) {getline a; print $0,a} else {print }}' file
它得到以&
结尾的行,并将它们与下一行连接起来。否则只需打印该行。
$ awk '{if ($0 ~ /&$/) {getline a; print $0,a} else {print }}' file
A7233334 PALLETS 2 11.000 EACH
2331 Storm Supply 247.5000 2,722.50 2,722.50
4144 Gentro Sales 225.0000 2,475.00 2,475.00
5155 Wacca Outfit 225.0000 2,475.00 2,475.00
6661 Acme Warehouse & Fuller Supply 225.0000 2,475.00 2,475.00
1661 McHugh & Donaldson 225.0000 2,475.00 2,475.00
2344 K-10 & Dunn Co 225.0000 2,475.00 2,475.00
6662 Ronaldson and Son Co. 225.0000 2,475.00
要删除多个空格或制作列,您可以使用| tr -s ' '
和| column
。
要删除前导空格,我们可以这样做:
awk '{if ($0 ~ /&$/) {getline a; gsub(/^[ \t]+|[ \t]+$/,"",a); print $0,a} else {print }}' file
基于https://stackoverflow.com/a/8766188/1983854。
测试:
$ awk '{if ($0 ~ /&$/) {getline a; gsub(/^[ \t]+|[ \t]+$/,"",a); print $0,a} else {print }}' file
A7233334 PALLETS 2 11.000 EACH
2331 Storm Supply 247.5000 2,722.50 2,722.50
4144 Gentro Sales 225.0000 2,475.00 2,475.00
5155 Wacca Outfit 225.0000 2,475.00 2,475.00
6661 Acme Warehouse & Fuller Supply 225.0000 2,475.00 2,475.00
1661 McHugh & Donaldson 225.0000 2,475.00 2,475.00
2344 K-10 & Dunn Co 225.0000 2,475.00 2,475.00
6662 Ronaldson and Son Co. 225.0000 2,475.00 2,475.00
A7233335 CRATES 2 1.000 EACH
2331 Storm Supply 147.5000 1,722.50 1,722.50
4144 Gentro Sales 125.0000 1,475.00 1,475.00
5155 Wacca Outfit 125.0000 1,475.00 1,475.00
6661 Acme Warehouse & Fuller Supply 125.0000 1,475.00 1,475.00
1661 McHugh & Donaldson 125.0000 1,475.00 1,475.00
2344 K-10 & Dunn Co 125.0000 1,475.00 1,475.00
6662 Ronaldson and Son Co. 125.0000 1,475.00 1,475.00
答案 1 :(得分:0)
最后.......做对了.........:D
代码:
i=1;
sed -i '/^$/d' zlist3 ; ## this is the reason it took me quite some time :( , the damm thing messes up the code if there are blank lines in between in input file ....
cat zlist3 | nl | grep "\&$" | tr -s ' ' | cut -c1-4 >> ztest5.csv ;
limit=`cat zlist3 | wc -l`;
while [ $i -le $limit ]
do
grep $i ztest5.csv ;
if [ $? -ne 0 ]
then
sed -n "${i}p" zlist3 | tr -s ' ' >> zlist4 ;
else
tmp1=`sed -n "${i}p" zlist3`;
i=$(($i+1));
tmp2=`sed -n "${i}p" zlist3`;
echo "$tmp1 $tmp2" | tr -s ' ' >> zlist4 ;
fi
i=$(($i+1));
done
如您所见,代码基于序列号...........
输出:
Kaizen~
$ cat zlist4
A7233334 PALLETS 2 11.000 EACH
2331 Storm Supply 247.5000 2,722.50 2,722.50
4144 Gentro Sales 225.0000 2,475.00 2,475.00
5155 Wacca Outfit 225.0000 2,475.00 2,475.00
6661 Acme Warehouse & Fuller Supply 225.0000 2,475.00 2,475.00
1661 McHugh & Donaldson 225.0000 2,475.00 2,475.00
2344 K-10 & Dunn Co 225.0000 2,475.00 2,475.00
6662 Ronaldson and Son Co. 225.0000 2,475.00 2,475.00
A7233335 CRATES 2 1.000 EACH
2331 Storm Supply 147.5000 1,722.50 1,722.50
4144 Gentro Sales 125.0000 1,475.00 1,475.00
5155 Wacca Outfit 125.0000 1,475.00 1,475.00
6661 Acme Warehouse & Fuller Supply 125.0000 1,475.00 1,475.00
1661 McHugh & Donaldson 125.0000 1,475.00 1,475.00
2344 K-10 & Dunn Co 125.0000 1,475.00 1,475.00
6662 Ronaldson and Son Co. 125.0000 1,475.00 1,475.00
.......我不再关心这个问题,完全喜欢编码......:D
哦顺便说一下,我希望这会有所帮助!!