如何用sed删除特定文件行?

sh/bash/dash/ksh/zsh等Shell脚本
回复
arthurchen
帖子: 8
注册时间: 2011-04-07 21:47

如何用sed删除特定文件行?

#1

帖子 arthurchen » 2011-04-25 16:27

需要删除一个文件中行长度超过20个字符的行,请问如何用sed实现
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: 如何用sed删除特定文件行?

#2

帖子 eexpress » 2011-04-25 16:31

行长度超过20个字符
/.{20,}/d
● 鸣学
头像
tenzu
论坛版主
帖子: 36924
注册时间: 2008-11-21 20:26

Re: 如何用sed删除特定文件行?

#3

帖子 tenzu » 2011-04-25 16:39

神是正则小王子 :em70
arthurchen
帖子: 8
注册时间: 2011-04-07 21:47

Re: 如何用sed删除特定文件行?

#4

帖子 arthurchen » 2011-04-25 21:19

eexpress 写了:行长度超过20个字符
/.{20,}/d
Thanks, but it doesn't work, I used sed -i -e '/.{20,}/d' file to remove the line contains more than 20 characters, is it right?
Can you please express the expression?
头像
lilydjwg
论坛版主
帖子: 4258
注册时间: 2009-04-11 23:46
系统: Arch Linux
联系:

Re: 如何用sed删除特定文件行?

#5

帖子 lilydjwg » 2011-04-25 23:47

arthurchen 写了:
eexpress 写了:行长度超过20个字符
/.{20,}/d
Thanks, but it doesn't work, I used sed -i -e '/.{20,}/d' file to remove the line contains more than 20 characters, is it right?
Can you please express the expression?
这个是超过20个字节的吧?那个 {} 貌似需要转义。
arthurchen
帖子: 8
注册时间: 2011-04-07 21:47

Re: 如何用sed删除特定文件行?

#6

帖子 arthurchen » 2011-04-26 19:20

不用sed了, 换成awk 'length < 20' 输出小于20个字符的行, 貌似这样速度还快些
因为我的是文件全是英文字符,所以字节和字符个数是一样的
头像
lexdene
帖子: 434
注册时间: 2010-02-21 16:19
来自: 大连
联系:

Re: 如何用sed删除特定文件行?

#7

帖子 lexdene » 2011-04-26 20:31

关注。
scutdk
帖子: 299
注册时间: 2009-07-25 19:23

Re: 如何用sed删除特定文件行?

#8

帖子 scutdk » 2011-05-08 2:22

要用sed的话,需要 sed -i -r -e 因为使用了ERE,所以加-r选项

Ubuntu 桌面培训- 全中文官方文档,含汉化截图,提供PDF
回复