如何在 BASH 中包含匹配模式的任何行之前添加 #?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24230117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:53:00  来源:igfitidea点击:

How to add a # before any line containing a matching pattern in BASH?

bashsedgrep

提问by Village

I need to add a #before any line containing the pattern "000", e.g., consider this sample file:

我需要#在包含模式“000”的任何行之前添加一个,例如,考虑这个示例文件:

This is a 000 line.
This is 000 yet ano000ther line.
This is still yet another line.

If I run the command, it should add #to the front of any files where "000" was found. The result would be this:

如果我运行该命令,它应该添加#到任何找到“000”的文件的前面。结果是这样的:

#This is a 000 line.
#This is 000 yet ano000ther line.
This is still yet another line.

The best I can do is a while loop, like this, which seems too complicated:

我能做的最好的是一个 while 循环,就像这样,它看起来太复杂了:

while read -r line
do
    if [[ $line == *000* ]]
    then
          echo "#"$line >> output.txt
    else
          echo $line >> output.txt
    fi
done < file.txt

How can I add a #to the front of any line where a pattern is found?

如何#在找到模式的任何行的前面添加一个?

采纳答案by potong

This might work for you (GNU sed):

这可能对你有用(GNU sed):

sed 's/.*000/#&/' file

回答by Tim Cooper

The following sed command will work for you, which does not require any capture groups:

以下 sed 命令将适用于您,它不需要任何捕获组:

sed /000/s/^/#/

Explanation:

解释:

  • /000/matches a line with 000
  • sperform a substitution on the lines matched above
  • The substitution will insert a pound character (#) at the beginning of the line (^)
  • /000/匹配一行 000
  • s在上面匹配的行上执行替换
  • 替换将#在行 ( ^)的开头插入一个井号( )

回答by geegee

the question was how to add the poundsign to those lines in a file so to make tim coopers excellent answer more complete i would suggest the following:

问题是如何将磅符号添加到文件中的这些行,以便使 tim coopers 出色的答案更完整,我建议如下:

sed /000/s/^/#/ > output.txt

or you could consider using sed's ability to in-place edit the file in question and also make a backup copy like:

或者您可以考虑使用 sed 的就地编辑相关文件的能力,并制作一个备份副本,如:

sed -i.bak /000/s/^/#/ file.txt

the "-i" option will edit and save the file inline/in-place the "-i.bak" option will also backup the original file to file.txt.bak in case you screw up something.

“-i”选项将编辑并内联/就地保存文件,“-i.bak”选项还将原始文件备份到 file.txt.bak 以防万一你搞砸了。

you can replace ".bak" with any suffix to your liking. eg: "-i.orignal" will create the original file as: "file.txt.orignal"

您可以根据自己的喜好将“.bak”替换为任何后缀。例如:“-i.orignal”将创建原始文件为:“file.txt.orignal”

回答by Baba

or use ed:

或使用ed

echo -e "g/000/ s/^/#/\nw\nq" | ed -s FILENAME

or open file in ed :

或在 ed 中打开文件:

ed FILENAME

and write this command

并写下这个命令

g/000/ s/^/#/
w
q

回答by Avinash Raj

Try this GNU sed command,

试试这个 GNU sed 命令,

$ sed -r '/000/ s/^(.*)$/#/g' file
#This is a 000 line.
#This is 000 yet ano000ther line.
This is still yet another line.

Explanation:

解释:

  • /000/sed substitution only works on the line which contans 000. Once it finds the corresponding line, it adds #symbol infront of it.
  • /000/sed 替换仅适用于包含000. 一旦找到相应的行,它就会#在其前面添加符号。

And through awk,

通过 awk,

$ awk '/000/{gsub (/^/,"#")}1' file
#This is a 000 line.
#This is 000 yet ano000ther line.
This is still yet another line
  • gsubfunction is used to add #infront of the lines which contains 000.
  • gsub函数用于在#包含000.