AWK : copy file with changes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK : copy file with changes
# 1  
Old 09-19-2009
Question 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.
# 2  
Old 09-19-2009
awk can do...
Code:
awk '!/^#/&&/ABC/&&/CONN/{gsub(/xyz/,"abc")}1' filename

# 3  
Old 09-19-2009
Hi Vidyadhar,

I will check that out. Thank You.
# 4  
Old 09-25-2009
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.
# 5  
Old 09-25-2009
Its working fine in AIX.. which OS are u using??
Code:
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)

# 6  
Old 09-25-2009
Quote:
Originally Posted by gowri_g_s
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.
use nawk on your system, will fix your problem.

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

# 7  
Old 09-25-2009
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk executes and works but only on copy of file

The tab-delimited file below using the awk produces a blank output. However, when I copy the same lines in file to a new document and execute the awk I get the desired result. The awk counts the unique characters before the : in $7 according to the id in $1. The awk seems to work but I can not... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. Shell Programming and Scripting

awk copy first column in new lines

Hi all i have table like this input001_1_174 j 474536 482492 mo001_1_175 j 960192 966656 .ire 966656 984416 .uf/i want copy number of first line to other line... (10 Replies)
Discussion started by: alii
10 Replies

3. Shell Programming and Scripting

Use sed/awk to do like copy and paste

I have rrd file which is have the gaps and I want to fill it out with some value , I've got 10 NaN record and I try to populate data from 10 records be for NaN to change instead of NaN :( <!-- 2016-05-19 14:10:00 CST / 1463638200 -->... (11 Replies)
Discussion started by: boobytrap
11 Replies

4. Shell Programming and Scripting

Copy entire file to a new file in awk

Hi, How do we write the contents of multiple files created in awk body to a new file in awk (END block) ? when I used "getline", it is consuming more time. Do we have any other way other than getline? (1 Reply)
Discussion started by: Cool
1 Replies

5. Shell Programming and Scripting

how to copy the directory but not copy certain file

Hi experts cp bin root src /mnt but not copy bin/bigfile any help? ( I post this thread in the "redhat" forum wrongly, I don't know how to withdraw that question in that wrong forum) Thanks (6 Replies)
Discussion started by: yanglei_fage
6 Replies

6. Shell Programming and Scripting

Copy respective path next to last column with awk

Hi to all, I have the short print out sample of the DOS command "dir S/" as showed below. Directory of C:\Program Files\Winamp\Skins\Bento\window 02/12/2010 11:35 p.m. <DIR> . 02/12/2010 11:35 p.m. <DIR> .. 11/12/2009 10:31 a.m. 13,556... (3 Replies)
Discussion started by: cgkmal
3 Replies

7. Shell Programming and Scripting

Perl or Awk script to copy a part of text file.

Hi Gurus, I'm a total newbie to Perl and Awk scripting. Let me explain the scenario, there is a DB2 table with 5 columns and one of the column is a CLOB datatype containing XML. We need all the 4 columns but only a portion of string from the XML column. We decided to export DB2 table to a .del... (26 Replies)
Discussion started by: asandy1234
26 Replies

8. Shell Programming and Scripting

copy/paste with awk

Hi everybody, I have two XML files. I am working on a script that could copy and paste the contents of the first xml file to the desired location in the second xml file. Here is my first XML file. This is the second XML file. Finaly, I wnat to obtain something like that : ... (2 Replies)
Discussion started by: lsaas
2 Replies

9. Shell Programming and Scripting

How to copy multiple lines from a file to another using AWK?

Hi, I have a abc.txt file. In this file there is a SQL query which Iwant to copy and print it on another file.The query (for eg) is written like this: SELECT field1, field2, field3 from table1,table2 where <conditions> END I want to copy this query to another... (3 Replies)
Discussion started by: jisha
3 Replies
Login or Register to Ask a Question