bash 如何使用 Sed 取消注释包含特定字符串的行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24889346/
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
How to UNCOMMENT a line that contains a specific string using Sed?
提问by JNLK
The lines in the file :
文件中的行:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 2000 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 2001 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 2002 -j ACCEPT
to comment out let's say the line that contains
注释掉让我们说包含的行
2001
i can simply run this SED command:
我可以简单地运行这个 SED 命令:
sed -i '/ 2001 /s/^/#/' file
but now how do i revert back ?
但现在我如何恢复?
as in uncomment that same line ?
就像取消注释同一行一样?
i tried
我试过
sed -i '/ 2001 /s/^//' file
that does not work.
这是行不通的。
采纳答案by Avinash Raj
Try this sed
command,
试试这个sed
命令,
sed -i '/^#.* 2001 /s/^#//' file
回答by JNLK
Yes, to comment line containing specific string with sed, simply do:
是的,要使用 sed 注释包含特定字符串的行,只需执行以下操作:
sed -i '/<pattern>/s/^/#/g' file
And to uncomment it:
并取消注释:
sed -i '/<pattern>/s/^#//g' file
In your case:
在你的情况下:
sed -i '/2001/s/^/#/g' file (to comment out)
sed -i '/2001/s/^#//g' file (to uncomment)
Option "g" at the end means global change. If you want to change only a single instance of pattern, just skip this.
最后的选项“g”表示全局变化。如果您只想更改模式的单个实例,请跳过此步骤。
回答by mklement0
To complement @Avinash Raj's helpful answerwith a more generic, POSIX-compliantsolution.
用更通用的、符合 POSIX 的解决方案来补充@Avinash Raj 的有用答案。
- Togglescommenting of linesthat match a specifiable stringthat must occur as a separate wordanywhere on the line.
- The comment character(string) is also specifiable.
- 切换与指定字符串匹配的行的注释,该字符串必须作为单独的单词出现在该行的任何位置。
- 该注释字符(串),也可指定。
Note that the solution is awk
-based, because a robust portablesolution with sed
is virtually impossible due to the limitations of POSIX' basicregular expressions.
请注意,该解决方案是awk
-based,因为由于 POSIX基本正则表达式的限制,几乎不可能实现强大的可移植解决方案。sed
awk -v commentId='#' -v word='2001' '
sed -i "" "/.*#.*d\/docker-php-ext-xdebug\.ini.*/s/^#//g" docker-compose.yml
~ "(^|[[:punct:][:space:]])" word "($|[[:punct:][:space:]])" {
if (match(umask 027
TMOUT=600
, "^[[:space:]]*" commentId))
# backup file (because we should always do this)
cp /etc/bash.bashrc /etc/bash.bashrc.$(date '+%Y-%m-%d,%H:%M:%S')
# original: TMOUT=600 , result :# TMOUT=600
sed -i '/[^#]/ s/\(^TMOUT=600.*$\)/#\ /' /etc/bash.bashrc
# original # TMOUT=600 ,result :TMOUT=600
sed -i '/^#.*TMOUT=600.*$/s/^#\ //' /etc/bash.bashrc
= substr(##代码##, RSTART + RLENGTH)
else
##代码## = commentId ##代码##
}
{ print }
' file > tmpfile.$$ && mv tmpfile.$$ file
(^|[[:punct:][:space:]])
and($|[[:punct:][:space:]])
are the POSIX extended regex equivalents of the\<
and\>
word-boundary assertions known from other regex dialects.- Whitespace afterthe comment char is preserved, but not before it.
- When prepending the comment char to a line, it is directly prepended, without whitespace.
- Thus, if you only toggle comments with thissolution, all whitespace is preserved.
- POSIX
awk
doesn't offer in-place updating (neither does POSIXsed
, incidentally), hence the output is first captured in a temporary file and that file then replaces the original on success.
(^|[[:punct:][:space:]])
和($|[[:punct:][:space:]])
是其他正则表达式方言中已知的\<
和\>
词边界断言的 POSIX 扩展正则表达式等价物。- 保留注释字符后的空白,但不保留它之前的空白。
- 当将注释字符添加到一行时,它是直接添加的,没有空格。
- 因此,如果您仅使用此解决方案切换注释,则会保留所有空格。
- POSIX
awk
不提供就地更新(sed
顺便说一句,POSIX也不提供),因此输出首先在临时文件中捕获,然后该文件在成功时替换原始文件。
回答by max4ever
For mac, which doesn't support standard sed parameters, this would remove the hashtag
对于不支持标准 sed 参数的 mac,这将删除主题标签
##代码##回答by Mike Q
Quick example of how to comment and uncomment a line in a file.
如何注释和取消注释文件中的行的快速示例。
Sample file :
示例文件:
##代码##Lets now backup the file (just for laughs) and comment out and un comment:
现在让我们备份文件(只是为了笑)并注释掉并取消注释:
##代码##