[英]How to remove the suffix of string in text file using shell script and sed?
I have a text file named file.txt with the following content. 我有一个名为file.txt的文本文件,其中包含以下内容。
customer
#line1-customer
security-team
#line1-security
I want only want to remove the # of #line1 under customer inside this file using shell script 我只想使用shell脚本删除此文件内客户下的#line1号#
My script 我的剧本
#!/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
After executing, no error was found. 执行后,未发现错误。 But i checked the file.txt and it was still the same (the # was not removed)
但是我检查了file.txt,它仍然是相同的(#未被删除)
How can i achieve my goal? 我如何实现我的目标?
If I understand correctly, you want to remove the first character of the line starting with the pattern contained in the variable string1
. 如果我理解正确,则要删除该行的第一个字符,该
string1
从变量string1
包含的模式开始。
If so, no need for bash script, a single sed script is enough: 如果是这样,则不需要bash脚本,一个sed脚本就足够了:
string1="#line1"
sed -i "/^${string1}/s/.\(.*\)$/\1/" file.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.