【发布时间】:2014-08-05 11:26:45
【问题描述】:
我需要在任何包含“000”模式的行之前添加一个#,例如,考虑这个示例文件:
This is a 000 line.
This is 000 yet ano000ther line.
This is still yet another line.
如果我运行该命令,它应该将# 添加到找到“000”的所有文件的前面。结果是这样的:
#This is a 000 line.
#This is 000 yet ano000ther line.
This is still yet another line.
我能做的最好的就是一个while循环,像这样,看起来太复杂了:
while read -r line
do
if [[ $line == *000* ]]
then
echo "#"$line >> output.txt
else
echo $line >> output.txt
fi
done < file.txt
如何在找到模式的任何行的前面添加#?
【问题讨论】:
-
您可以使用 sed 命令来完成此操作,但您是否考虑过使用文本编辑器执行此操作?例如,在 vim 中,这项任务可以通过宏轻松自动化。