Search and replace

Is there a way to do search _and_ replace on the command line? the grep family are fine for doing matches, but they don't so substitutions. I can write up a Perl script, but it seems such a fundimental task for an OS to do (Red Hat 6.0).
ccavnorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
Search and replace can be done with perl, sed, or awk on the command line.
0
tmossCommented:
sed 's/regexp/replacement/g' input.file > output.file

This say to search the file input.file for the regular expression and substitute the replacement string then write the results to output.file. The g option means global - substitue every occurance, not just the first.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mliberiCommented:
Using sed you can also specify more than one search/replace couple, but the input file will be re-read from beginning to end for each couple.

That behaviuor leads to unexpteced result if a replace part of a couple is present in the search part of another couple.

Example:
search xx, substitute with yy
search yy, substitute with zz

Result: all xx occurrences AND all yy occurrences replaced with zz

To avoid this I developed a program that can do multiple search/replace in a SINGLE PASS. It is called 'opmsr' an acronym for One Pass Multiple Search Replace. You can download it from my home page at the following URL: http://www.geocities.com/mliberi

enjoy
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux Distributions

From novice to tech pro — start learning today.