sed - 注释与特定字符串匹配且尚未注释掉的行 [英] sed - Commenting a line matching a specific string AND that is not already commented out
本文介绍了sed - 注释与特定字符串匹配且尚未注释掉的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下测试文件
AAA
BBB
CCC
使用以下 sed 我可以注释掉 BBB 行.
Using the following sed I can comment out the BBB line.
# sed -e '/BBB/s/^/#/g' -i file
我只想注释掉开头没有 # 的行.
I'd like to only comment out the line if it does not already has a # at the begining.
# sed -e '/^#/! /BBB/s/^/#/g' file
sed: -e expression #1, char 7: unknown command: `/'
有什么想法可以实现这一目标吗?
Any ideas how I can achieve this?
推荐答案
假设您没有包含多个 #
的任何行,这将起作用:
Assuming you don't have any lines with multiple #
s this would work:
sed -e '/BBB/ s/^#*/#/' -i file
注意:您不需要/g,因为您每行最多进行一次替换.
Note: you don't need /g since you are doing at most one substitution per line.
这篇关于sed - 注释与特定字符串匹配且尚未注释掉的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文