I'm writing a bash script on Cent OS7. Now I need to use sed to remove all lines which don't contain .jpg or .jpeg.
Here is my script:
sed -i -e '/\.jp(e)?g/!d' myfile
But it will delete all lines, which means that it doesn't work as expected.
However, if I do sed -i -e '/\.jpg/!d' myfile or sed -i -e '/\.jpeg/!d' myfile. Both of them work well.
sed Q uits input just as soon as the very first match is found anywhere in the file - which means it does not waste any more time continuing to read the infile. sed returns in its exit code the numeric value 0 or 1 depending on whether it couldn't/could find the addressed regexp. $i is set to sed 's return upon exit.
If no match is found, the variable will be set as i=0. Show activity on this post. Show activity on this post. sed (Stream EDitor) is not the right tool for this kind of things: you can simply use a bash if statement to match your input against a regex:
Some facts about this answer: sed Quits input just as soon as the very first match is found anywhere in the file - which means it does not waste any more time continuing to read the infile. sed returns in its exit code the numeric value 0 or 1 depending on whether it couldn't/could find the addressed regexp.
sed Q uits input just as soon as the very first match is found anywhere in the file - which means it does not waste any more time continuing to read the infile. sed returns in its exit code the numeric value 0 or 1 depending on whether it couldn't/could find the addressed regexp.
Captured group (()) and the quantifier ? (match the preceding token 0 or 1 time) comes (at least) with ERE (Extended RegEx), not BRE (Basic RegEx).
sed by default uses BRE, so the tokens are being treated literally.
To enable ERE, use -E (or -r if available) with sed:
sed -E '/\.jp(e)?g/!d' myfile
Capturing e is redundant here:
sed -E '/\.jpe?g/!d' myfile
Note that, you can use ERE tokens from BRE by escaping them with \, so the following would work too:
sed '/\.jp\(e\)\?g/!d' myfile
sed '/\.jpe\?g/!d' myfile
Again, this does not look as clean as just using one option i.e. -E. Only case where you will want this is portability.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With