Problem facing in using awk command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem facing in using awk command
# 1  
Old 05-24-2010
Problem facing in using awk command

Hi.,

I am not able to replace the string with another string using gsub fn of awk command.

My code:

Code:
 
awk 'BEGIN gsub([^|]004,IND,004)' p.txt

and my i/p file p.txt is of the format:
Code:
 
av|004|adkf|Kent,004|s
av|005|ssdf|Kd,IT park|s
.
.
.

and my desired o/p should be of :
Code:
 
av|IND|adkf|Kent,004|s
av|005|ssdf|Kd,IT park|s
.
.
.


Request you to help me on this.


Thanks.,
# 2  
Old 05-24-2010
Code:
awk -F\| '{gsub("004","IND",$2)}1' infile

# 3  
Old 05-24-2010
Hi.,

Code:
$ awk -F\| '{gsub("004","IND",$2)}1' p.txt


I tried with the above command, but I got this as the error, when I executed in shell:



Code:
 
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: bailing out near line 1

Here I want you to suggest to execute the command, as I am new to working with Shell scripts.


Thanks.,
# 4  
Old 05-24-2010
Try with nawk if you are on Solaris...

Code:
nawk -F\| '{gsub("004","IND",$2)}1' p.txt

# 5  
Old 05-24-2010
Try this:

Code:
awk -F "|" '{OFS="|";gsub("004","IND",$2)}1' filename



cheers,
Devaraj Takhellambam
# 6  
Old 05-24-2010
Hi.,

Code:
nawk -F\| '{gsub("004","IND",$2)}1' p.txt

The above command is successfully replacing the code, but it is taking out the seperator away from the file.

It is resulting in :

Code:
av IND adkf Kent,004 s
av|005|ssdf|Kd,IT park|s

as opposed to my desired o/p:

Code:
av|IND|adkf|Kent,004|s
av|005|ssdf|Kd,IT park|s


Request you to provide the solution for this. And also as per my i/p file, is there any way to replace full file with all code list with all county list, by reading list i/p from a file?

Like in.,

my county_code.txt file

Code:
004,IND
005,SL
006,BNG
007,PAK
008,AFG

my i/p file:


Code:
av|004|adkf|Kent,004|s
av|005|ssdf|Kd,IT park|s
.
.
.

and my o/p should be of format:


Code:
av|IND|adkf|Kent,004|s
av|SL|ssdf|Kd,IT park|s
.
.
.



---------- Post updated at 04:10 AM ---------- Previous update was at 04:08 AM ----------

For the cmd.,

Code:
awk -F "|" '{OFS="|";gsub("004","IND",$2)}1' p.txt

It is again resulting in the following err:

a
Code:
wk: syntax error near line 1
awk: bailing out near line 1

Request you to give a work around for my more general problem, of replacing whole file at a stretch.

Thanks.,
# 7  
Old 05-24-2010
Try something like this:

Code:
IFS=,;
cp filename filename_bkp
while read str rep
 do
 nawk -F\| -v str=$str -v rep=$rep '{OFS="|";gsub(str,rep,$2)}1' filename
done <file_str| sort -u


cheers,
Devaraj Takhellambam

Last edited by devtakh; 05-24-2010 at 06:23 AM.. Reason: replace awk with nawk
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. AIX

I'm facing problem with rpm command, when running the command and appears this error:

exec(): 0509-036 Cannot load program /usr/opt/freeware/bin/rpm because of the following errors: 0509-022 Cannot load module /opt/freeware/lib/libintl.a(libintl.so.1). 0509-150 Dependent module /opt/freeware/lib/libiconv.a(shr4.o) could not be loaded. 0509-152 Member... (4 Replies)
Discussion started by: Ohmkar
4 Replies

2. Shell Programming and Scripting

Scripting with awk: facing problem

Hi, I wants to print the 9th column information with its path name in some txt file. Here is one line which works fine for me: rfdir /castor/cern.ch/user/s/sudha/forPooja | grep data | awk '{print "rfio:///castor/cern.ch/user/s/sudha/forPooja/"$9}' > dataFilenames.list rfdir=="ls -ltr" ... (2 Replies)
Discussion started by: nrjrasaxena
2 Replies

3. AIX

facing problem using su

Hi, I am able to login using su - or su directly , # prompt is coming, it doesnt ask for password. any normal user on aix system is login using su - or su . Please suggest where to change the configuration direct root login is disabled in /etc/ssh/sshd_config file. (0 Replies)
Discussion started by: manoj.solaris
0 Replies

4. Shell Programming and Scripting

Problem facing command using shell

Dear Brothers! Need your help for the case where I am running one command on prompt and its giving us the correct output, but when i use the same command from shell its directs no output.:wall: the command on command prompt is ls -ltrh * | nawk '{if ($5~ '/$'M'/') print $9}' | grep -v... (1 Reply)
Discussion started by: jojo123
1 Replies

5. Solaris

Facing problem with zone

i am using this way to create zone1 and zone2 bash-2.05b# zonecfg -z zone1 zone1: No such zone configured Use 'create' to begin configuring a new zone. zonecfg:zone1> create zonecfg:zone1> set zonepath=/zone/1 zonecfg:zone1> set autoboot=true zonecfg:zone1> add net zonecfg:zone1:net>... (6 Replies)
Discussion started by: coxmanchester
6 Replies

6. Shell Programming and Scripting

Problem facing with sed and awk

Hi All, I have a got a problem .. I have t files as below: 1.txt contains ----- ----- ----- column 1, "cat", column 24, "dog", column 100, "rat", ----- ----- ----- 2.sh should contain ----- ----- ----- awk 'BEGIN { printf ("%1s","cat")}' (19 Replies)
Discussion started by: jisha
19 Replies

7. Solaris

please help as i am facing problem with uptime

Hi I am getting the uptime output as follows 12:40am up 4 day(s), 18:29, 2 users, load average: 38.97, 36.54, 34.89 The load average is too high . I have checked the processes , but no process is taking too much cpu time Please help (3 Replies)
Discussion started by: guy009
3 Replies

8. UNIX for Dummies Questions & Answers

facing problem with cut command

hi , i used ls -ltr | cut -f 1 > \dev\tty but all teh coulmns r getting printed instead of only one........how can i resolve this? prob 2 : wud be able start cutting from last field......supposing in the case of dyanmic list.i dunno the field number of last column.......so is... (3 Replies)
Discussion started by: vivekshankar
3 Replies

9. UNIX for Dummies Questions & Answers

facing a problem in redirection

Hi, I am doing this perl script print (@line(1..15)); the lines 1 to 15 get printed... how can i redirect this to file? thanks and regards vivek.s (4 Replies)
Discussion started by: vivekshankar
4 Replies
Login or Register to Ask a Question