[英]How to remove the suffix of string in text file using shell script and sed?
我有一个名为file.txt的文本文件,其中包含以下内容。
customer
#line1-customer
security-team
#line1-security
我只想使用shell脚本删除此文件内客户下的#line1号#
我的剧本
#!/bin/bash
string1="#line1-customer"
prefix=#
cat file.text | while read LINE
do
if [ "$LINE" == "$string1" ]; then
sed -i 's/"$LINE"/"$LINE#"$prefix""/g' file.text
done
执行后,未发现错误。 但是我检查了file.txt,它仍然是相同的(#未被删除)
我如何实现我的目标?
如果我理解正确,则要删除该行的第一个字符,该string1
从变量string1
包含的模式开始。
如果是这样,则不需要bash脚本,一个sed脚本就足够了:
string1="#line1"
sed -i "/^${string1}/s/.\(.*\)$/\1/" file.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.