AWK : copy file with changes

Hi,

I am seeking help to copy a file - with the changes (not just the changes) - to a new file. I have a text file that contains numerous lines of text as given below -

Name ABC.DEF.GHI Adrs (tcp) Port (53) CONN(xyz)
#Name ABC.DEF.GHI Adrs (tcp) Port (53) CONN(xyz)
Name GHI.JKL.MNO Adrs (tcp) Port (53) CONN(abc)
#Name GHI.JKL.MNO Adrs (tcp) Port (53) CONN(abc)

I wish to copy the <infile> to <outfile> with some changes, say using the following -
grep ABC <file> | grep CONN | grep -v "#" | sed /s/xyz/abc > [outfile]

But the above line copies only the fetched & amended data to the new file. But, I want to copy all the lines + the amendments that I make.

Can someone help me on this? Thanks a bunch in advance.

awk can do...

awk '!/^#/&&/ABC/&&/CONN/{gsub(/xyz/,"abc")}1' filename

Hi Vidyadhar,

I will check that out. Thank You.

Hi,

No the above awk line did not work. It throws "awk : syntax error". However, I removed the gsub function and used {print $0} instead and it returns the correct line. But, substitution is NOT working. Can u plz check ?

Thanks a lot.

Its working fine in AIX.. which OS are u using??

home/> cat vv
Name ABC.DEF.GHI Adrs (tcp) Port (53) CONN(xyz)
#Name ABC.DEF.GHI Adrs (tcp) Port (53) CONN(xyz)
Name GHI.JKL.MNO Adrs (tcp) Port (53) CONN(abc)
#Name GHI.JKL.MNO Adrs (tcp) Port (53) CONN(abc)
home/> awk '!/^#/&&/ABC/&&/CONN/{gsub(/xyz/,"abc")}1' vv
Name ABC.DEF.GHI Adrs (tcp) Port (53) CONN(abc)
#Name ABC.DEF.GHI Adrs (tcp) Port (53) CONN(xyz)
Name GHI.JKL.MNO Adrs (tcp) Port (53) CONN(abc)
#Name GHI.JKL.MNO Adrs (tcp) Port (53) CONN(abc)

use nawk on your system, will fix your problem.

nawk '!/^#/&&/ABC/&&/CONN/{gsub(/xyz/,"abc")}1'

Hi Vidhyadhar,
I use Solaris 10.

----------------

Hi rdcwayx,
nawk worked well in the example above.

But, It did not work with the following line,
DEFINE CHNL(TO.ABC.DEF) CHLTYPE(SDR) XMITQ(ABC.TRANS.Q) TRPTYPE(tcp) CONNAME('abcghij.pqr.com(1010)') REPLACE

I wanted to repalce the abcghij after CONNAME to say, pqrstuv.
I really cant figure it out why it did not work. Sorry for the trouble.

Thanks a bunch to both of you.

what command did you tried to accomplish that?? can u post that??

Hi rdcwayx,

Never mind. I found out that I had committed a syntax error.
Its working fine now.
Thanks a lot to you and to Vidhyadhar.
Have a great time.