1

我有一个文本文件,其中包含我需要摆脱的换行符。它有多个块,每个块之间有一个空行。它的格式如下:

    A7233334 托盘 2 11.000 每个
 2331 风暴补给 247.5000 2,722.50 2,722.50
 4144 Gentro 销售 225.0000 2,475.00 2,475.00
 5155 瓦卡装 225.0000 2,475.00 2,475.00
 6661 Acme 仓库 &
           富勒供应 225.0000 2,475.00 2,475.00
 1661 麦克休 & 唐纳森 225.0000 2,475.00 2,475.00
 2344 K-10 &
           邓恩公司 225.0000 2,475.00 2,475.00
 6662 Ronaldson and Son Co. 225.0000 2,475.00 2,475.00

A7233335 板条箱 2 1.000 每个 2331 风暴补给 147.5000 1,722.50 1,722.50 4144 Gentro 销售 125.0000 1,475.00 1,475.00 5155 瓦卡装 125.0000 1,475.00 1,475.00 6661 Acme 仓库 & 富勒供应 125.0000 1,475.00 1,475.00 1661 麦克休和唐纳森 125.0000 1,475.00 1,475.00 2344 K-10 & 邓恩公司 125.0000 1,475.00 1,475.00 6662 罗纳德森父子公司 125.0000 1,475.00 1,475.00

我需要用换行符解开这些行,以便它输出到一个新文件,如下所示:

    A7233334 托盘 2 11.000 每个
 2331 风暴补给 247.5000 2,722.50 2,722.50
 4144 Gentro 销售 225.0000 2,475.00 2,475.00
 5155 瓦卡装 225.0000 2,475.00 2,475.00
 6661 Acme Warehouse & Fuller Supply 225.0000 2,475.00 2,475.00
 1661 麦克休 & 唐纳森 225.0000 2,475.00 2,475.00
 2344 K-10 和邓恩公司 225.0000 2,475.00 2,475.00
 6662 Ronaldson and Son Co. 225.0000 2,475.00 2,475.00

A7233335 板条箱 2 1.000 每个 2331 风暴补给 147.5000 1,722.50 1,722.50 4144 Gentro 销售 125.0000 1,475.00 1,475.00 5155 瓦卡装 125.0000 1,475.00 1,475.00 6661 Acme Warehouse & Fuller Supply 125.0000 1,475.00 1,475.00 1661 麦克休和唐纳森 125.0000 1,475.00 1,475.00 2344 K-10 和邓恩公司 125.0000 1,475.00 1,475.00 6662 罗纳德森父子公司 125.0000 1,475.00 1,475.00

有任何想法吗?

4

2 回答 2

1

这个 awk 一个班轮可以做到:

$ 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
于 2013-06-04T15:56:00.577 回答
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

如您所见,代码基于序列号............

输出 :

改善~

$ 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

哦,顺便说一句,我希望这会有所帮助!

于 2013-06-04T18:59:11.280 回答