繁体   English   中英

在前一行中将n行最多追加到某个字符

[英]Append n lines up to a certain character in the previous line

输入:

[5 8 15 79 5 6  
.  
.  
.   
n 8 6 4 5 ]  
[18 78 18 79 5 6  
.  
.  
.   
n 8 6 78 ]

等等...

所需的输出:

[5 8 15 79 5 6 . . . . 8 6 4 5 ]  
[18 78 18 79 5 6 . . .  8 6 78 ]

我需要的所有列转换多达]成一条线,并继续这样做直到文件的末尾。

听起来好像您只想在当前行以]结尾时才打印换行符。 尝试:

awk '{printf "%s%s", $0, match($0,"]\\s*$") ? "\n" : ""}' input

与sed:

$ sed ':a;N;/\]/!ba;s/\n//g' infile
[5 8 15 79 5 6  .  .  .   n 8 6 4 5 ]
[18 78 18 79 5 6  .  .  .   n 8 6 78 ]

解释:

:a        # Label to jump to
N         # Append next line to pattern sapce
/\]/! ba  # If there is no "]", jump to label
s/\n//g   # Remove all newlines (only reached if "]" in pattern space)

如果要确保]是行中的最后一个非空白字符,而不仅仅是行中的任何地方,则可以将正则表达式从/\\]/更改为/\\][[:blank:]]*$/ ,导致

sed ':a;N;/\][[:blank:]]*$/!ba;s/\n//g' infile

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM