使用 Sed 替换包含字符串的整行 [英] Replace whole line containing a string using Sed

查看:250
本文介绍了使用 Sed 替换包含字符串的整行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件,它有一个特定的行

I have a text file which has a particular line something like

sometext sometext sometext TEXT_TO_BE_REPLACED sometext sometext sometext

我需要用

This line is removed by the admin.

搜索关键字为TEXT_TO_BE_REPLACED

我需要为此编写一个shell脚本.如何使用 sed 实现这一点?

I need to write a shell script for this. How can I achieve this using sed?

推荐答案

您可以使用 change 命令替换整行,并使用 -i 标志使就地更改.例如,使用 GNU sed:

You can use the change command to replace the entire line, and the -i flag to make the changes in-place. For example, using GNU sed:

sed -i '/TEXT_TO_BE_REPLACED/c\This line is removed by the admin.' /tmp/foo

这篇关于使用 Sed 替换包含字符串的整行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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