Discussion Locked

Pattern search and replacement in shell

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
I am helping to set up a production AIX UNIX box in my shop. We have several SAS scripts that will be running monthly and will need monthly changes to their code. The SAS code is in the form of a text file, so I think we will be able to accomplishthese changes using a shell script. I know I can locate the patterns that need changing using awk, but I have not come across anything that will replace them for me once found. There must be a way, and I'm sure it's not difficult. 200 easy tech points to anyone who can set me up with a solution. -Thanks

3 replies

by stephen darlington

Use the right tool for the job!

If you want to replace one string literal with another, sed will do the trick, e.g.

sed 's/this/that/' < in > out

sed also does regular expressions, but it can get complicated very quickly. (What does 's/Acct#\([0-9]*\)/\1/' do? Answers on a postcard...)

You can do similar tricks with awk (same command). The main advantage is that you can remember fields from previous lines more easily.

Remember, Unix In A Nutshell is your friend.

And don't forget Perl or Python if it's more complicated than that.

This discussion is closed to new replies.