4

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

http://s.thebrighttag.com/iframe?c=A5lqOqP
http://s.wordpress.com/imgpress?resize=136,136&url=http%3A%2F%2Fdata.whicdn.com%2Fimages%2F2833524%2F4thofJuly-collage_large.jpg%3F1278084193

How do I remove those lines from the file?

4 Answers 4

14
cat file | grep -v "\.png" >new_file_without_pngs

Updated for comment:

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

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.

3
  • 5
    How about using the sed delete command 'd': sed -i -r '/\.(png|jpg)$/I d' file
    – frielp
    Commented Aug 26, 2011 at 8:16
  • 3
    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
    Commented Aug 26, 2011 at 9:08
  • 3
    +1 to the "use d" comment: using s/.*blah.*// will leave blank lines, it won't delete lines. Commented Aug 26, 2011 at 10:23
1

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

0

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .