我有一个大文件,其中大多数行很长,但有几行只是几个字:
Here is a long sentence. This is a short line. Here is a long sentence. Here is a long sentence. This is another short line. Here is a long sentence.
所有短线都以独特的单词开头。在此示例中,短行以“This”开头,以“line”结尾。它们的行数完全相同。我需要一个命令来连接只文件中的短行,以便我得到
Here is a long sentence. This is a short line. Here is a long sentence. Here is a long sentence. This is another short line. Here is a long sentence.
是否有命令可以完成这项工作?如果我不需要...我不想写脚本程序。
感谢。
答案 0 :(得分:4)
perl -pe's/\n/ / if ($j ||= /^This$/) &&= !/^line\.$/'
用法:
perl -pe'...' file.in >file.out # From file
perl -pe'...' <file.in >file.out # From STDIN
perl -i~ -pe'...' file # "In-place" with backup
perl -i -pe'...' file # "In-place" without backup
答案 1 :(得分:0)
sed -e :a -e '/line\.$/{p;d;}' -e '/^This/{N;s/\n/ /g;};ta' file.in > file.out