Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to swap strings in files 1

Status
Not open for further replies.

mrmac228

IS-IT--Management
May 27, 2003
255
GB
I have 3 files, a template file, one with old strings that exist in the template file and one with new string I want to replace the old strings with in the template file.

e.g. template

1,testold1,3,4
2,testold2,3,4
3,testold3,3,4
4,testold4,3,4
5,testold1,3,4

oldstrings
testold1
testold2
testold3
testold4

newstrings
testnew1
testnew2
testnew3
testnew4

I want to end up with a new file, based on the contents of the template file such that the new file looks like

1,testnew1,3,4
2,testnew2,3,4
3,testnew3,3,4
4,testnew4,3,4
5,testnew1,3,4

What i've tried always ends up with having to deal with a number of intermediate files, is there any slicker way to do this?

thnx!


I love deadlines. I like the whooshing sound they make as they fly by - Douglas Adams
 

There are many solutions, but you can try this one:
Code:
paste -d' ' oldstrings newstrings|awk '{print "s/"$1"/"$2"/g";}'|sed -f - template
[3eyes]

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Now there is the command I need.

I'm running on Solaris, so the command needed to be split into 2 as sed doesn't like taking terminal input on this flavour of UNIX, but still thats the sort of slick 1 liners I like.

THANKS!



I love deadlines. I like the whooshing sound they make as they fly by - Douglas Adams
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top