我有一个文本文件,其中有一行
sometext sometext sometext TEXT_TO_BE_REPLACED sometext sometext sometext
我需要将上面的整行替换为
This line is removed by the admin.
搜索关键字是text_to_be_replace
我需要为此写一个shell脚本。如何使用sed实现这一点?
我有一个文本文件,其中有一行
sometext sometext sometext TEXT_TO_BE_REPLACED sometext sometext sometext
我需要将上面的整行替换为
This line is removed by the admin.
搜索关键字是text_to_be_replace
我需要为此写一个shell脚本。如何使用sed实现这一点?
当前回答
您可以使用change命令替换整行,并使用-i标志进行适当的更改。例如,使用GNU sed:
sed -i '/TEXT_TO_BE_REPLACED/c\This line is removed by the admin.' /tmp/foo
其他回答
下面的命令对我有用。哪个是处理变量的
sed -i "/\<$E\>/c $D" "$B"
你需要在前后使用通配符(.*)来替换整行:
sed 's/.*TEXT_TO_BE_REPLACED.*/This line is removed by the admin./'
cat find_replace | while read pattern replacement ; do
sed -i "/${pattern}/c ${replacement}" file
done
Find_replace文件包含2列,c1为匹配模式,c2为替换,sed循环替换包含变量1的模式之一的每一行
您可以使用change命令替换整行,并使用-i标志进行适当的更改。例如,使用GNU sed:
sed -i '/TEXT_TO_BE_REPLACED/c\This line is removed by the admin.' /tmp/foo
这和上面那个很相似。
sed 's/[A-Za-z0-9]*TEXT_TO_BE_REPLACED.[A-Za-z0-9]*/This line is removed by the admin./'