微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

用多行文本replace单行

在Linux中,我可以使用什么命令来用新的多行代替单行文本? 我想在一行中查找一个关键字并删除这一行,并用多个新行replace它。 因此,在下面显示的文本中,我想要search包含“关键字”的行,并用如图所示的3行新文本replace整行。

例如,replace包含关键字的行,

This is Line 1 This is Line 2 that has keyword This is Line 3

改为:

This is Line 1 Inserted is new first line Inserted is new second line Inserted is new third line This is Line 3

删除3年以前的文件

在Linux中用邮件或者mutt替代发送邮件

在电子邮件输出格式不错

按另一列的值计算一个列组的值的数量

如何匹配文件扩展名,而不pipebash脚本中的大小写

尝试在共享内存中写入时发生总线错误

将html转换为xls或xlsx的Libreoffice

bash脚本执行从上次失败的地方

如何在使用gcc进行静态链接时仅包含已使用的符号?

如何编辑在Linux / bash中使用&&排队的进程?

$ sed '/keyword/c > Inserted is new first line > Inserted is new second line > Inserted is new third line' input.txt This is Line 1 Inserted is new first line Inserted is new second line Inserted is new third line This is Line 3

$和>是bash提示

创建一个文件script.sed ,其中包含:

/keyword/{i Inserted is new first line Inserted is new second line Inserted is new third line d }

将其应用于您的数据:

sed -f script.sed your_data

如何做到这一点,使用c和a命令,而不是i和/或d ,但是这是相当干净的。 它找到关键字,插入三行数据,然后删除包含关键字的行。 ( c命令完成了这一切,但我不记得它存在,并且a命令附加了文本,并且在本文中与i基本上是同义词。)

你也可以使用shell buildins来完成它:

STRING1_WITH_MULTIPLE_LInes="your text here" STRING2_WITH_MULTIPLE_LInes="more text" OUTPUT="" while read LINE || [ "$LINE" ]; do case "$LINE" in "Entire line matches this")OUTPUT="$OUTPUT$STRING1_WITH_MULTIPLE_LInes ";; *"line matches this with extra before and/or after"*)OUTPUT="$OUTPUT$STRING2_WITH_MULTIPLE_LInes ";; *)OUTPUT="$OUTPUT$LINE ";; esac done < file echo "$OUTPUT" >file

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐