Problem facing in using awk command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem facing in using awk command
# 8  
Old 05-24-2010
Code:
nawk -F "|" '{gsub("004","IND",$2)}1' OFS="|" filename

This User Gave Thanks to malcomex999 For This Post:
# 9  
Old 05-24-2010
Hi.,



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

It is resulting in output:



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


And also as per my i/p file, is there any way to replace full file with all code list with all county list(located only at a specific column), by reading list i/p from a file, using similar type of command?

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
.
.
.


Actually I tried with.,

Code:
 
cat country_code.txt | while read line ; do  
     code="$(echo "$line" | cut -d ',' -f1)"
     country="$(echo "$line" | cut -d ',' -f2)"
 nawk -F "|" '{gsub("$code","$country",$2)}1' OFS="|" p.txt > output1.txt
done


But it is not replacing the code with country.Smilie

Request you to suggest a solution for this, for replacing a specific field of every record.


Thanks.,

Last edited by av_vinay; 05-24-2010 at 08:44 AM..
# 10  
Old 05-24-2010
Did you try to use the solution I provided earlier. You should be able to tweak it a little bit .
# 11  
Old 05-24-2010
Ya.,

I tried with the things, but again it is resulting in the same error:

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


As I mentioned earlier, awk with -F option is not working for me.

So I tried with nawk command, as above. I am able to seperate the fields from the file, but it is not replacing as a whole. But if I hardcode it is working fine. Request you to give the solution for the same.


Thanks.,
# 12  
Old 05-24-2010
Code:
IFS=,;
while read str rep; 
do 
awk -F "|" -v str=$str -v rep=$rep '$2 == str {gsub(str,rep,$2);print}' OFS="|" file2 ; done < file3


cheers,
Devaraj Takhellambam
This User Gave Thanks to devtakh For This Post:
# 13  
Old 05-24-2010
Here is what works as you are requesting w/explanations...

awk '{sub("004","IND")}{print}' p.txt

Notes:
1. gsub is for replacing ALL occurrences of a pattern in an input line, sub just the first one
2. The "^" character designates the beginning of a line, which is not what you are dealing with here
3. BEGIN is only for actions to be performed prior to acting on the input file
4. The square brackets "[]" are used for array subscripts

Hope this helps.

Jim MacKelvey
jimbmac777@comcast.net
This User Gave Thanks to jimbmac777 For This Post:
# 14  
Old 05-25-2010
Hi all.,

I am facing problem using awk with its associated functions.
So I tried with sed command and it worket for me.

Here is the working script:

Code:
 
#!/bin/bash

echo "Enter input file "
read fname
TFILE=$fname
 

cat country_code.txt | while read line ; do  
     code="$(echo "$line" | cut -d ',' -f1)"
     country="$(echo "$line" | cut -d ',' -f2)"
     cat "$TFILE"|cut -f4 | sed -e 's/'"$code"'/'"$country"'/1' > "${TFILE}".tmp
     mv $"${TFILE}.tmp" "$TFILE"
     echo LINE=${line} CODE=${code} COUNTRY=${country}   
done

Thanks for your support and suggestions.

Regards.,
Vinay
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