sed 将 # 替换为空行
sed replace # with empty line
如何使用 sed 将只有 # 的行替换为空行?
我试图在 google 上找到,但我什么也没找到。
文件内容:
#test
another test
#
another test2
预期结果:
#test
another test
another test2
所以,在预期的结果下,经过另一次测试,该行应该是没有#的空白。
非常感谢任何帮助。
使用正则表达式,您可以匹配 ^
行的开头和 $
行的结尾。 s/regexp/replacement/
命令将用 replacement
.
替换匹配 regexp
的文本
此 sed
命令给出了所需的输出:
sed 's/^#$//' < input.txt
在每一行上,sed
查找行首、#
字符,然后是行尾,然后将其替换为空。但是,换行符仍然存在,因此您只剩下一个空行。
sed '/^#$//'
- 锚点到行的开头 (
^
) 和结尾 ($
) 以精确匹配整行。
使用sed
$ sed '/[[:alnum:]]/ ! s/#//' file
#test
another test
another test2
这可能适合您 (GNU sed):
sed '/^#$/g' file
如果一行只包含#
,用空行替换它。
备选方案:
sed 's/^#$//' file
或
sed '/^#$/c\' file
如何使用 sed 将只有 # 的行替换为空行?
我试图在 google 上找到,但我什么也没找到。
文件内容:
#test
another test
#
another test2
预期结果:
#test
another test
another test2
所以,在预期的结果下,经过另一次测试,该行应该是没有#的空白。
非常感谢任何帮助。
使用正则表达式,您可以匹配 ^
行的开头和 $
行的结尾。 s/regexp/replacement/
命令将用 replacement
.
regexp
的文本
此 sed
命令给出了所需的输出:
sed 's/^#$//' < input.txt
在每一行上,sed
查找行首、#
字符,然后是行尾,然后将其替换为空。但是,换行符仍然存在,因此您只剩下一个空行。
sed '/^#$//'
- 锚点到行的开头 (
^
) 和结尾 ($
) 以精确匹配整行。
使用sed
$ sed '/[[:alnum:]]/ ! s/#//' file
#test
another test
another test2
这可能适合您 (GNU sed):
sed '/^#$/g' file
如果一行只包含#
,用空行替换它。
备选方案:
sed 's/^#$//' file
或
sed '/^#$/c\' file