basic sed - search and replace

light-blue
light-blue used Ask the Experts™
on
How do I write a sed command, run through crontab (I'm doing crontab -e as root) and search for

http://www.company.com/somewhere

and replace with

<!-- BEGIN IDS -->{ID}<!-- END IDS -->http://www.company.com/somewhere

Thanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
sed 's#\(http://www\.company\.com/somewhere\)#<!- BEGIN IDS -->{ID}<!- END IDS -->\1#'
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
sed -i.bak -e 's#http://www.company.com/somewhere#<!-- BEGIN IDS -->{ID}<!-- END IDS -->&#' file

Commented:
Typo.

sed 's#\(http://www\.company\.com/somewhere\)#<!- BEGIN IDS -->{ID}<!- END IDS -->\1#'
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

Commented:
Typo.

sed 's#\(http://www\.company\.com/somewhere\)#<!-- BEGIN IDS -->{ID}<!-- END IDS -->\1#'

Author

Commented:
Hmmm...it seems that none of those worked. I even tried changing the original file to have just aaabbb and running

sed 's#\(aaabbb\)#<!- BEGIN IDS -->{ID}<!- END IDS -->\1#' filename.htm

but no replacing happened.

Maybe it would more helpful (?) to say I want to replace the entire a html element tag so

http://www.somewhere.com/somplace/script.php 

becomes

<!-- BEGIN IDS -->{ID}<!-- END IDS -->http://www.company.com/someplace/somescript.php

Author

Commented:
or, more likely, I'm doing something (basic) wrong. maybe if I tried a simpler example of sed, I could figure out what I'm doing incorrectly...

Commented:
> sed 's#\(aaabbb\)#<!- BEGIN IDS -->{ID}<!- END IDS -->\1#' filename.htm
> but no replacing happened.

Try escaping "!"
Eg:

sed 's#http://www.company.com/somewhere#<\!-- BEGIN IDS -->{ID}<\!-- END IDS -->&#' file

Author

Commented:
Thank you both! They worked perfectly, but I wasn't redirecting STDOUT properly. Duh!

Thanks again!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial