【发布时间】:2019-06-28 14:21:00
【问题描述】:
我想使用命令来编辑文件的特定行,而不是使用 vi。这就是事情。如果有以该行开头的#,则替换# 以取消注释。否则,添加 # 以使其评论。我想使用 sed 或 awk。但它不会按预期工作。
这是file。
what are you doing now?
what are you gonna do? stab me?
this is interesting.
This is a test.
go big
don't be rude.
例如,我只想在第 4 行 This is a test 的开头添加 #,如果它不以 # 开头。如果它以# 开头,则删除#。
我已经通过 sed & gawk (awk) 尝试过
gawk -i inplace '$1!="#" {print "#",$0;next};{print substr($0,3,length-1)}' file
sed -i /test/s/^#// file # make it uncomment
sed -i /test/s/^/#/ file # make it comment
我不知道如何使用if else 使sed 工作。我只能用一个命令来完成它,然后使用另一个正则表达式来做相反的事情。
使用 gawk,它作为主线工作。但它会弄乱其余的代码。
【问题讨论】: