预期:
Input:
###
# comment
###
var1=/opt
#var2=/app
Output:
comment
var1=/opt
var2=/app
我尝试运行一些代码,但它没有按实际顺序打印:
grep "var1" cf.cfg
grep "#" cf.cfg | cut -d "#" -f2
Output using these codes:
var1=/opt
comment
var2=/app
尝试以下命令。
sed '/^[ #]*$/d;s/^[ #]*//' cf.cfg
它消除了..
线只有#
或任何组合空间。
从线开始,#
或任何组合的空间,包括线路为# # comment
。
您可以使用
sed -e 's/#s*//;/^s*$/d' yourfile
这将删除#
字符和后续的Whitespace(s/#s*//
(和包含非Whitespace字符的打印行(/^s*$/d
(。
如果形式的线
var=1 # this is a comment
上线将打印
如果要在#
之前删除字符,则可以使用
sed -e 's/^[^#]*#+s*//;/^s*$/d' yourfile
var=1 this is a comment
这将删除所有文本到第一个#
。