Using awk array problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using awk array problem
# 1  
Old 01-11-2012
Using awk array problem

I am trying to map values in the input file, where 2nd column depends on the specific value in the 1st column. When 1st column is A place 1 into 2nd column, when it is B, place 2, when C place 3, otherwise no change.

My input:
Code:
U |100|MAIN ST   |CLMN1|1
A |200|GREEN LN  |CLMN2|2
1 |12 |THIRD ST  |CLMN3|3

Intended output:
Code:
U |100|MAIN ST   |CLMN1|1
A |1|GREEN LN  |CLMN2|2
1 |12 |THIRD ST  |CLMN3|3


My code:
Code:
gawk -F'|' '
BEGIN{
        map["A"] = 1;
        map["B"] = 2;
        map["C"] = 3;
        OFS = "|";
}
{
        s = "not found"
        if ($1 in map) {
                $2 = map[$1];
                s = "Found:" $1 " : " map[$1];
        }
        print $0, s;
}
END{
        OFS = ":";
        print "Just checking my array:"
        for (a in map) { print a, map[a] }
}
'

The problem is, letter "A" in the 2nd row is not found in the array somehow.
I added printing my map array out and 'found/not found' to check myself, nothing is found.
The output:

Code:
 
U |100|MAIN ST   |CLMN1|1|not found
A |200|GREEN LN  |CLMN2|2|not found
1 |12 |THIRD ST  |CLMN3|3|not found
Just checking my array:
A:1
B:2
C:3

Any ideas what I do wrong?

Just a note: there are many ways to do it, I'd like to keep present way of mapping values initialized in the array, there might be a possibility for untrained people to mainain the mapping in the future.
# 2  
Old 01-11-2012
Because the value you have in field 1 is "A " not "A"
This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 01-11-2012
Code:
[root@node2 ~]# cat get 
awk -F'|' '{
            if($1 ~ /A/)
               $2=1
            if($1 ~ /B/)
               $2=2
            if($1 ~ /C/)
               $2=3
            OFS="|"
            print $0
           }' $1

              
[root@node2 ~]# bash get data 
U |100|MAIN ST   |CLMN1|1
A |1|GREEN LN  |CLMN2|2
1 |12 |THIRD ST  |CLMN3|3
B |2|core  ST  |CLMN3|4
C |3|THIRD ST  |CLMN3|3
[root@node2 ~]# cat data 
U |100|MAIN ST   |CLMN1|1
A |200|GREEN LN  |CLMN2|2
1 |12 |THIRD ST  |CLMN3|3
B |800|core  ST  |CLMN3|4
C |12 |THIRD ST  |CLMN3|3

# 4  
Old 01-11-2012
As I said in my note: I am looking for a solution where the mapping is not in the code, but rather in the array in the BEGIN section, so other people who do not know awk would be able to easy change the mapping.
# 5  
Old 01-12-2012
You already got an answer from ChublerXL, it is because $1 is "A " not "A". If you want to keep your present code, I suggest you preprocess your data to not contain spaces before or after the pipe delimiter:
Code:
sed 's/ *| */|/g' dataFile | awk '{...

Actually, there is a more elegant way; just set the delimiter to " *| *" instead of "|". That way it will eat the spaces.
Code:
 awk -F =" *| *" '{...

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Index problem in associate array in awk

I am trying to reformat the table by filling any missing rows. The final table will have consecutive IDs in the first column. My problem is the index of the associate array in the awk script. infile: S01 36407 53706 88540 S02 69343 87098 87316 S03 50133 59721 107923... (4 Replies)
Discussion started by: yifangt
4 Replies

2. Shell Programming and Scripting

How to Assign an shell array to awk array?

Hello All, Can you please help me with the below. #!/bin/bash ARR="No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5 EXTRACT_DT:30-SEP-12 VER_NUM:1" ARR="No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5... (14 Replies)
Discussion started by: Ariean
14 Replies

3. Shell Programming and Scripting

Problem with awk array when loading from shell variable

Hi, I have a problem with awk array when iam trying to use awk in solaris box as below..Iam unable to figure out the problem.. Need your help. is there any alternative to make it in arrays from variable values nawk 'BEGIN {SUBSEP=" "; split("101880|110045 101887|110045 101896|110045... (9 Replies)
Discussion started by: cskumar
9 Replies

4. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

5. Shell Programming and Scripting

awk array problem

Hi, Im trying to count bats flying through an infrared beam array. One of the experts here helped me a few months ago but now I am having a problem that is stumping me. here is the original code that works (with two differnt patterns in array): # this has been changed to operate under the... (15 Replies)
Discussion started by: cmp260
15 Replies

6. Shell Programming and Scripting

Challenging Awk array problem

Hi, I rather have a very complicated awk problem here, at least to me. I have two files. File 1: 607 687 174 0 0 chr1 3000001 3000156 -194195276 - L1_Mur2 LINE L1 -4310 1567 1413 1 607 917 214 114 45 chr1 3000237 ... (19 Replies)
Discussion started by: polsum
19 Replies

7. Shell Programming and Scripting

AWK help. how to compare a variable with a data array in AWK?

Hi all, i have a data array as follows. array=ertfgj2345 array=456ttygkd . . . array=errdjt3235 so number or elements in the array can varies depending on how big the data input is. now i have a variable, and it is $1 (there are $2, $3 and so on, i am only interested in $1). ... (9 Replies)
Discussion started by: usustarr
9 Replies

8. Shell Programming and Scripting

AWK Array problem

Dear All, I am facing problem to get right output through awk program I have file in which “B” value is appearing multiple time and I need to capture all these values. My script is BEGIN { FS=" " } { if ( substr($1,1,5) == "START" ) { i =... (2 Replies)
Discussion started by: arvindng
2 Replies

9. Shell Programming and Scripting

Problem with lookup values on AWK associative array

I'm at wits end with this issue and my troubleshooting leads me to believe it is a problem with the file formatting of the array referenced by my script: awk -F, '{if (NR==FNR) {a=$4","$3","$2}\ else {print a "," $0}}' WBTSassignments1.txt RNCalarms.tmp On the WBTSassignments1.txt file... (2 Replies)
Discussion started by: JasonHamm
2 Replies

10. Shell Programming and Scripting

awk array problem

hi i am trying to perform some calculations with awk and arrays. i have this so far: awk 'NR==FNR{ for(i=1; i<=NF; i++) {array+=$i} tot++;next} {for(i=1; i<=NF; i++) {avg=array/tot} {diff=(array - avg)}} {for(i=1; i<=NF; i++) {printf("%5.8f\n",diff)}}' "$count".txt "$count".ttt >... (4 Replies)
Discussion started by: npatwardhan
4 Replies
Login or Register to Ask a Question