sed:取决于第四个连接线 [英] sed: joining lines depending on the fourth one

查看:71
本文介绍了sed:取决于第四个连接线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件,偶尔会有分行符.

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

.....

我想重新加入分割线:



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
.....

使用sed.我不清楚如何与前面的一行加入一条线. 有什么建议吗?

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

推荐答案

sed用于单个行的简单替换,仅此而已.对于其他任何事情,您都应该使用awk:

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

.....

以上内容将在具有所有POSIX兼容awks的所有UNIX系统上可靠,可移植且有效地工作.

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

这篇关于sed:取决于第四个连接线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆