gpt4 book ai didi

linux - 如何连接与模式匹配的行?

转载 作者:太空宇宙 更新时间:2023-11-04 05:37:59 25 4
gpt4 key购买 nike

我有一个巨大的文件,其中大多数行都很长,但有几行只有几个单词:

Here is a long sentence.Thisisashortline.Here is a long sentence.Here is a long sentence.Thisisanothershortline.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.

是否有一个命令可以完成这项工作?如果不需要的话我不想写脚本程序...

谢谢。

最佳答案

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

关于linux - 如何连接与模式匹配的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15584115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com