簡體   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