sed: joining lines depending on the fourth one

拟墨画扇 提交于 2020-01-17 08:43:19

问题


I have a file that, occasionally, has split lines.

The split is signaled by the fact that two consecutive lines with Alphabetic characters.


5

00:00:00,000 --> 00:00:00,000

Alphabetic characters
Alphabetic characters

6

00:00:00,000 --> 00:00:00,000

Alphabetic characters

7

00:00:00,000 --> 00:00:00,000

Alphabetic characters
Alphabetic characters

8

00:00:00,000 --> 00:00:00,000

Alphabetic characters

.....

I'd like join the split line back:


5

00:00:00,000 --> 00:00:00,000

Alphabetic characters Alphabetic characters

6

00:00:00,000 --> 00:00:00,000

Alphabetic characters

7

00:00:00,000 --> 00:00:00,000

Alphabetic characters Alphabetic characters

8

> 00:00:00,000 --> 00:00:00,000

Alphabetic characters Alphabetic characters
.....

using sed. I'm not clear how to join a line with the preceeding one. Any suggestion?


回答1:


sed is for simple subsitutions on individual lines, that is all. For anything else you should be using awk:

$ awk '/[[:alpha:]]/{ if (buf=="") {buf=$0; next} else {$0=buf OFS $0; buf=""} } 1' file

5

00:00:00,000 --> 00:00:00,000

Alphabetic characters Alphabetic characters

6

00:00:00,000 --> 00:00:00,000


7

00:00:00,000 --> 00:00:00,000

Alphabetic characters Alphabetic characters

8

00:00:00,000 --> 00:00:00,000

Alphabetic characters Alphabetic characters

.....

The above will work robustly, portably, and efficiently on all UNIX systems with all POSIX-compatible awks.




回答2:


sed '$!{N;/^[a-zA-Z ][^\n]\+\n[a-zA-Z ]/{s/\n/ /}}'

Match two lines back-to-back that meet the condition that the first line starts with an alphabetic character or space, and the second starts with the same. Join them with a space.




回答3:


Another approach with sed:

sed '/^[[:alpha:]]/{N;/\n[[:alpha:]]/s/\n/ /}' file

When a line starting with alphabetic characters is found, add next line to the pattern space using the N command. Then replace newline when followed by alphabetic characters with a space.



来源:https://stackoverflow.com/questions/40685028/sed-joining-lines-depending-on-the-fourth-one

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!