Need help with search/replace using grep/sed

Megan's picture

She has: 11,421 posts

Joined: Jun 1999

Hello,

I have another grep problem! This time i'm trying to search/replace an image file name throughout multiple files recursively. I've gotten this far:

grep -l "littlecampaignlogo" 2009/*/*.html | xargs sed -i 's/littlecampaignlogo.gif/littlecampaignlogo2009.gif'

The problem is what to do with the . in the file name? Sed is returning an error.

Thanks for any help!!

P.S. It would be great if someone could write an article on some of these commands for A Padded Cell!

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

I think you just need to escape the . with backslash.

's/littlecampaignlogo\.gif/littlecampaignlogo2009\.gif'

Megan's picture

She has: 11,421 posts

Joined: Jun 1999

Thanks, Greg. If I do this:

grep -l "littlecampaignlogo" 2009/*/*.html | xargs sed -i 's/littlecampaignlogo\.gif/littlecampaignlogo2009\.gif'

It gives me the error "sed: -e expression #1, char 53: unterminated `s' command"

Megan's picture

She has: 11,421 posts

Joined: Jun 1999

Stupid error, just needed a closing slash on the sed command:

grep -l "littlecampaignlogo" 2009/*/*.html | xargs sed -i 's/littlecampaignlogo\.gif/littlecampaignlogo2009\.gif/'

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.