简体   繁体   English

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

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

Input: 输入:

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

and so on... 等等...

Desired output: 所需的输出:

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

I need to convert all the columns up to ] into a single line and keep on doing so up to the end of the file. 我需要的所有列转换多达]成一条线,并继续这样做直到文件的末尾。

It sound like you just want to only print a newline if the current lines ends with ] . 听起来好像您只想在当前行以]结尾时才打印换行符。 Try: 尝试:

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

With sed: 与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 ]

Explained: 解释:

: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)

If you want to make sure that ] is the last non-blank character on the line, not just anywhere in it, the regex can be changed from /\\]/ to /\\][[:blank:]]*$/ , resulting in 如果要确保]是行中的最后一个非空白字符,而不仅仅是行中的任何地方,则可以将正则表达式从/\\]/更改为/\\][[:blank:]]*$/ ,导致

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

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

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