我有一个偶尔会有分割线的文件。
分裂由两个连续的具有字母字符的行组成。
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。我不清楚如何加入前一行。 有什么建议吗?
答案 0 :(得分:1)
Food(String food) {
// need `this` to refer to instance variable food since there's
// scope overlap.
this.food = food;
}
背靠背匹配两条线,满足第一条线以字母字符或空格开头的条件,第二条线以相同的方式开始。加入他们的空间。
答案 1 :(得分:1)
sed的另一种方法:
sed '/^[[:alpha:]]/{N;/\n[[:alpha:]]/s/\n/ /}' file
当找到以字母字符开头的行时,使用N
命令将下一行添加到模式空间。然后在后跟带空格的字母字符时替换换行符。
答案 2 :(得分:1)
sed用于单个行上的简单替换,即全部。对于其他任何你应该使用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的UNIX系统上稳健,便携,高效地工作。