linux文本删除命令行,linux命令行输出文本_如何在Linux上的命令行中从文本文件中删除特定行?...

linux命令行输出文本

On Linux, how to delete a specific line from a text file in command line? For example, to delete the 4th line from a file

在Linux上 ,如何在命令行中从文本文件中删除特定行? 例如,从文件中删除第四行

aaa

bbb

ccc

ddd

eee

ffffff

You can use the “stream editor for filtering and transforming text” sed.

您可以使用“流编辑器来过滤和转换文本” sed 。

With GNU sed:

使用GNU sed:

sed -i '4d' ./file

Here, -i means edit the file inplace. d is the command to “delete the pattern space; immediately start next cycle”. 4 means the 4th line.

在这里, -i表示就地编辑文件。 d是“删除模式空间 ; 立即开始下一个周期”。 4表示第四行。

The file content will be:

文件内容为:

aaa

bbb

ccc

eee

ffffff

There are more combinations for deleting lines. Some examples are:

还有更多删除行的组合。 一些例子是:

Remove the last line:

删除最后一行:

sed '$d' filename.txt

Remove all empty lines:

删除所有空行:

sed '/^$/d' ./file

or

要么

sed '/./!d' ./file

Remove lines from 7 to 9:

从7到9删除行:

sed '7,9d' ./file

Remove the line matching by a regular expression REGULAR:

用正则表达式REGULAR删除匹配的行:

sed '/REGULAR/d' ./file

For a simple example, remove the lines containing “oops”:

举一个简单的例子,删除包含“ oops”的行:

sed '/oops/d' ./file

Answered by Eric Z Ma.

埃里克·马(Eric Z Ma)回答。

翻译自: https://www.systutorials.com/how-to-delete-a-specific-line-from-a-text-file-in-command-line-on-linux/

linux命令行输出文本

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值