Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've a list of urls of which some point to images:


How do I remove those lines from the file?

share|improve this question
up vote 14 down vote accepted
cat file | grep -v "\.png" >new_file_without_pngs

Updated for comment:

egrep -iv "\.(png|jpg|jpeg|gif|etc)" file >new_file
share|improve this answer
no inline option? I'd like to get rid of ".png" and ".jpg" for that matter. Also case-insenstive? – simpatico Aug 26 '11 at 4:00
I would add a $ at end, just in case. – enzotib Aug 26 '11 at 7:47

For the in-place editing variant, you can use sed like the following:

$ sed -i -r 's/.*\.(png|jpg).*//I' file

It will remove the lines containing either .png or .jpg directly in the file. The -i option passed to sed means 'in-place editing'. Make a backup of your file before executing this command, or try it first without the -i option if you are unsure.

The I flag is to perform a case-insensitive matching.

share|improve this answer
How about using the sed delete command 'd': sed -i -r '/\.(png|jpg)$/I d' file – frielp Aug 26 '11 at 8:16
you can add an extension to -i so that it will create a backup file: sed -i .bak -r 's/.*\.(png|jpg).*//I' file – Julian Aug 26 '11 at 9:08
+1 to the "use d" comment: using s/.*blah.*// will leave blank lines, it won't delete lines. – glenn jackman Aug 26 '11 at 10:23

You can use Vim in Ex mode:

ex -sc 'g/\.png/d' -cx file
  1. g global search

  2. d delete

  3. x save and close

share|improve this answer

If you have sponge you can swap out the contents of a file in place like this:

grep -E -iv "\.(png|jpg|jpeg|gif)" file | sponge file
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.