追加不包含“|”的行到上一行

追加不包含“|”的行到上一行

我有一个文本文件,其中包含以下格式的数据。

1|0|this is test file line1
2|1|this is test file line2
3|1|this
is
test
file line4

任何不包含的行|都应附加到包含的前一行|

输出:

1|0|this is test file line1
2|1|this is test file line2
3|1|this is test file line4

答案1

一种方法是使用 awk 实现以下算法:

  • 跟踪上一行prev
  • 如果该行包含|,并且不是第一行,则打印prev。之后,将当前行存储在prev
  • 如果该行不包含|,则将其附加到prev
  • 在脚本末尾,打印prev

例如这样:

awk '/\|/ { if (NR > 1) print prev; prev=$0 }
     !/\|/ { prev = prev $0 }
     END { print prev }' input

答案2

用作|字段分隔符:如果该行包含 a,|则该NF变量将大于 1。

awk -F'|' 'NR > 1 && NF > 1 {print ""} {printf "%s", $0} END {print ""}' file

答案3

awk '/\|/ { if (printed==1) print ""; else printed=1;
    printf "%s",$0; next; }; { printf " %s",$0 }; END { print ""; }' inputfile

或者,如果您不关心前导换行符,则更短:

awk '/\|/ { printf "\n%s",$0; next; }; { printf " %s",$0 }; END { print ""; }' inputfile

答案4

还有一个 awk:

awk -F'|' 'NR>1{printf prev (NF>1?"\n":" ")}{prev=$0}END{print prev}' file

测试

$ cat file1
1|1|this is test file line1
2|2|this is test file line2
3|3|this
is
test
file line3
4|4|this is test file line4
5|5|this is
test file
line5
6|6|this is test file line6

$ awk -F'|' 'NR>1{printf prev (NF>1?"\n":" ")}{prev=$0}END{print prev}' file1
1|1|this is test file line1
2|2|this is test file line2
3|3|this is test file line3
4|4|this is test file line4
5|5|this is test file line5
6|6|this is test file line6 

相关内容