如何使用 Linux/Unix 将文本文件中以某个字符开头的行替换为空行?
我正在使用 Ubuntu,并且有一个大型文本文件,其中某些行以 <
字符开头,我想将其中的每一个替换为空行。因此,
eeeeee
<
<
aaaa
bbbb
cccc
<
dddddd
<
ff
我想要这样:(
eeee
aaaa
bbbb
cccc
dddddd
ff
如果有多个连续 <
行,理想情况下只需要一个空行)
如何在命令行中执行此操作?
I'm using Ubuntu and have a large text file where certain rows start with <
character what I'd like to replace each of these to an empty row. So from this:
eeeeee
<
<
aaaa
bbbb
cccc
<
dddddd
<
ff
I want this:
eeee
aaaa
bbbb
cccc
dddddd
ff
(In case of multiple consecutive <
rows only one empty rows is needed ideally)
How to perform this in command line?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个 Perl 一行代码应该可以满足您的要求:
它正在运行:
注释代码:
This Perl one-liner should do what you're asking for:
Here it is in action:
Commented code:
你可以使用 sed。在这种情况下,命令将类似于:
sed '/
这将找到带有“<”的行字符开头,并将该行替换为空。但是,它不会用单行替换多个空行。
You could use sed. In this case the command would look something like:
sed '/</c\\' file.txt
This would find a line with a '<' character at the beginning, and replace the line with nothing. It would not however replace multiple empty rows with a single row.