我需要#
在包含模式"000"的任何行之前添加一个,例如,考虑这个示例文件:
This is a 000 line.
This is 000 yet ano000ther line.
This is still yet another line.
Run Code Online (Sandbox Code Playgroud)
如果我运行该命令,它应该添加#
到找到"000"的任何文件的前面.结果是这样的:
#This is a 000 line.
#This is 000 yet ano000ther line.
This is still yet another line.
Run Code Online (Sandbox Code Playgroud)
我能做的最好的是像这样的while循环,这似乎太复杂了:
while read -r line
do
if [[ $line == *000* ]]
then
echo "#"$line >> output.txt
else
echo $line >> output.txt
fi
done < file.txt
Run Code Online (Sandbox Code Playgroud)
如何#
在找到模式的任何行的前面添加一个?
Tim*_*per 59
以下sed命令适用于您,不需要任何捕获组:
sed /000/s/^/#/
Run Code Online (Sandbox Code Playgroud)
说明:
/000/
匹配一条线 000
s
在上面匹配的行上执行替换#
在行的开头插入一个字符()(^
)pot*_*ong 23
这可能适合你(GNU sed):
sed 's/.*000/#&/' file
Run Code Online (Sandbox Code Playgroud)