使用 sed 忽略注释 (#),但保持行不变 [英] Ignore comments (#) using sed, but keep the lines untouched

查看:72
本文介绍了使用 sed 忽略注释 (#),但保持行不变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个配置文件,我想基本上编辑未注释的行,而不是注释的行.我正在使用 sed.

例如,我有一个名为 file.txt 的文件:

<预>测试# 测试# 测试测试

我想用TEST"替换test",但不要触摸注释行.最终输出应显示:

测试# 测试# 测试测试

解决方案

sed '/^#/!s/test/TEST/g'/path/to/infile

输出

$ sed '/^#/!s/test/TEST/g' infile测试# 测试# 测试测试

*注意:如果您对评论的唯一要求是第一个非空白字符是 #,那么您可以使用:

sed '/^[[:space:]]*#/!s/test/TEST/g'/path/to/infile

输出

$ sed '/^[[:space:]]*#/!s/test/TEST/g' infile测试# 测试# 测试测试

I have a config file that I want to basically edit the uncommented lines, but not the commented lines. I'm using sed.

For example, I have a file called file.txt:

test
# test
# test
test

I want to replace "test" with "TEST" but do NOT touch the commented lines. Final output should show:

TEST
# test
# test
TEST

解决方案

sed '/^#/!s/test/TEST/g' /path/to/infile

Output

$ sed '/^#/!s/test/TEST/g' infile
TEST
# test
# test
TEST

*Note: If your only requirement for a comment is that the very first non-whitespace character is a #, then you can use:

sed '/^[[:space:]]*#/!s/test/TEST/g' /path/to/infile

Output

$ sed '/^[[:space:]]*#/!s/test/TEST/g' infile
TEST
# test
 # test
TEST

这篇关于使用 sed 忽略注释 (#),但保持行不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆