如何将特定列向下移动一行?

时间:2015-09-11 11:14:53

标签: bash shell awk sed

我有以下文本文件:

 Ca1     
      2.75766949          2.49500070          0.77962056
 Ca2     
      5.83334907          4.99000000          3.81528863
 Ca3     
      0.00000000         -0.00000000         22.02929701
 Ca4     
      0.81321980          2.49500070          6.85095669
 Ca5     
      3.88889938          4.99000000          9.88662475
 Ca6     
      6.96457896          2.49500070         12.92229282
 Ca7     
      1.94444969          4.99000000         15.95796089

我想知道“Ca”列是否可以向下移动1行以获得:

 Ca1     2.75766949          2.49500070          0.77962056

 Ca2     5.83334907          4.99000000          3.81528863

 Ca3     0.00000000         -0.00000000         22.02929701

 Ca4     0.81321980          2.49500070          6.85095669

 Ca5     3.88889938          4.99000000          9.88662475

 Ca6     6.96457896          2.49500070         12.92229282

 Ca7     1.94444969          4.99000000         15.95796089

我尝试了

  

AWK

但是,我找不到解决方案。

提前致谢。

4 个答案:

答案 0 :(得分:2)

使用awk

awk 'NR%2!=0{prev=$0;next} {print prev, $0}' yourfile

使用sed

sed 'N; s/\n//g' yourfile

答案 1 :(得分:1)

这个awk单行检查文本(Ca模式)而不是行号来连接行。

 awk '{printf "%s%s", $0, (/Ca/?"":RS)}' file

如果您想查看行号,请将/Ca/?更改为NR%2?

答案 2 :(得分:1)

$ awk '{ORS=(NF>1?"\n":"")}1' file
 Ca1           2.75766949          2.49500070          0.77962056
 Ca2           5.83334907          4.99000000          3.81528863
 Ca3           0.00000000         -0.00000000         22.02929701
 Ca4           0.81321980          2.49500070          6.85095669
 Ca5           3.88889938          4.99000000          9.88662475
 Ca6           6.96457896          2.49500070         12.92229282
 Ca7           1.94444969          4.99000000         15.95796089

$ awk '{ORS=(NF>1?"\n\n":"")}1' file
 Ca1           2.75766949          2.49500070          0.77962056

 Ca2           5.83334907          4.99000000          3.81528863

 Ca3           0.00000000         -0.00000000         22.02929701

 Ca4           0.81321980          2.49500070          6.85095669

 Ca5           3.88889938          4.99000000          9.88662475

 Ca6           6.96457896          2.49500070         12.92229282

 Ca7           1.94444969          4.99000000         15.95796089

答案 3 :(得分:0)

之间的空行
sed '/^Ca[0-9]/ {N;s/\(\n\)\(.*\)/\2\1/;}' YourFile

wihtout

sed '/^Ca[0-9]/ {N;s/\n//;}' YourFile