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:

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

and my i/p file p.txt is of the format:

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

and my desired o/p should be of :

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

Request you to help me on this.

Thanks.,

awk -F\| '{gsub("004","IND",$2)}1' infile

Hi.,

$ 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:

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

Try with nawk if you are on Solaris...

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

Try this:

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

cheers,
Devaraj Takhellambam

Hi.,

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 :

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

as opposed to my desired o/p:

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

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

my i/p file:

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

and my o/p should be of format:

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

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

It is again resulting in the following err:

a

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

Try something like this:

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

nawk -F "|" '{gsub("004","IND",$2)}1' OFS="|" filename
1 Like

Hi.,

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

It is resulting in output:

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

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

my i/p file:

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

and my o/p should be of format:

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

Actually I tried with.,

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

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

Thanks.,

Did you try to use the solution I provided earlier. You should be able to tweak it a little bit .

Ya.,

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

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

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

1 Like

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

1 Like

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:

 
#!/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