Delete the line with a sepific occurance pattern in SED?
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.
精彩评论