删除 SED 中具有特定出现模式的行?
请花几秒钟。我想删除第六次出现
的行。 我用谷歌搜索了这样做的方法,并尝试了sed -i '/^
$/ d/6' file
。然而,结果是
sed: -e expression #1, char 9: extra characters after
错误的。
Please spend a few second. i would like to delete the line which has the sixth <ul>
occurance.
i google the way of doing it and i tried
sed -i '/^<ul>$/ d/6' file
.
However, it turn out a
sed: -e expression #1, char 9: extra characters after
error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
AFAIK 你不能用
sed
轻松做到这一点。您可以这样做,例如将所有行连接到一个字符串,然后删除第 6 次出现,然后拆分字符串,但使用awk
更容易:测试结果在这里:https://ideone.com/3YpVu
注意:它会丢弃第 6 个整行
,并假设每行只能找到一个
。
华泰
AFAIK you can't do that easily with
sed
. You can do that like joining all the lines to one string, then delete that 6th occurence, then split the string, but it's easier withawk
:Test results are here: https://ideone.com/3YpVu
Note: it discards the whole line with the 6th
<ul>
, and assumes that there are only one<ul>
can be found per line.HTH
perl -p -i -e "s/^$\n//g"
应该删除与您给出的 perl 正则表达式匹配的任何行在任何现代 UNIX 系统上。否则,如果您正在计算出现次数并终止第 6 次出现 的行,我知道没有任何命令可以在一行中执行此操作。perl -p -i -e "s/^<perl-regex-here>$\n//g" <filename>
should get rid of any line matching the perl regular expression you give on any modern unix system. Otherwise, if you are counting occurences and kill the line with the 6th occurrence of , I know of no command that will do that in one line.