开发者

Delete the line with a sepific occurance pattern in SED?

开发者 https://www.devze.com 2023-03-21 07:32 出处:网络
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

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

开发者_Go百科
sed: -e expression #1, char 9: extra characters after 

error.


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 with awk:

awk '/<ul>/ {ul++} ul == 6 { getline } 1' INPUTFILE > TMPFILE && mv TMPFILE INPUTFILE

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


awk '/<ul>/ && ++count == 6 {next} {print}' filename


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消