Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
add comment

3 Answers

up vote 12 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
add comment

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
5  
How about using the sed delete command 'd': sed -i -r '/\.(png|jpg)$/I d' file –  frielp Aug 26 '11 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 Aug 26 '11 at 9:08
3  
+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
add comment

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
add comment

Your Answer

 
discard

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.