Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading 2 input files but not getting expected value 1

Status
Not open for further replies.

pdtak

Technical User
Feb 25, 2008
63
US
I'm reading 2 input files but not getting expected value.
I should get an alpha value on file_1_data[*] but not getting any.

Please help.

>cat test6.sh
awk '
FILENAME==ARGV[1] { file_1_data[$1]=$0; print "----- 1 Line " NR " -----" $1; next }
FILENAME==ARGV[2] { file_2_data[$1]=$0; print "----- 2 Line " NR " -----" $1; next }
END {
# do actual data processing here
print "----- Line " NR " -----"
print "----- file_1_data[1] " file_1_data[1]
print "----- file_1_data[2] " file_1_data[2]
print "----- file_1_data[3] " file_1_data[3]
print "----- file_1_data[4] " file_1_data[4]
print "----- file_1_data[5] " file_1_data[5]
print "----- file_1_data[6] " file_1_data[6]
print "----- file_1_data[7] " file_1_data[7]
print "----- file_1_data[8] " file_1_data[8]
print "----- file_1_data[9] " file_1_data[9]
print "----- file_1_data[10] " file_1_data[10]
print "----- file_2_data[1] " file_2_data[1]
print "----- file_2_data[2] " file_2_data[2]
print "----- file_2_data[3] " file_2_data[3]
print "----- file_2_data[4] " file_2_data[4]
print "----- file_2_data[5] " file_2_data[5]
print "----- file_2_data[6] " file_2_data[6]
print "----- file_2_data[7] " file_2_data[7]
print "----- file_2_data[8] " file_2_data[8]
print "----- file_2_data[9] " file_2_data[9]
print "----- file_2_data[10] " file_2_data[10]
print "\n"
}
' infile1 infile2

>cat infile1
A
B
C
D
E
F
G
H
I
J

>cat infile2
1
2
3
4
5
6
7
8
9
10

>./test6.sh
----- 1 Line 1 -----A
----- 1 Line 2 -----B
----- 1 Line 3 -----C
----- 1 Line 4 -----D
----- 1 Line 5 -----E
----- 1 Line 6 -----F
----- 1 Line 7 -----G
----- 1 Line 8 -----H
----- 1 Line 9 -----I
----- 1 Line 10 -----J
----- 2 Line 11 -----1
----- 2 Line 12 -----2
----- 2 Line 13 -----3
----- 2 Line 14 -----4
----- 2 Line 15 -----5
----- 2 Line 16 -----6
----- 2 Line 17 -----7
----- 2 Line 18 -----8
----- 2 Line 19 -----9
----- 2 Line 20 -----10
----- Line 20 -----
----- file_1_data[1]
----- file_1_data[2]
----- file_1_data[3]
----- file_1_data[4]
----- file_1_data[5]
----- file_1_data[6]
----- file_1_data[7]
----- file_1_data[8]
----- file_1_data[9]
----- file_1_data[10]
----- file_2_data[1] 1
----- file_2_data[2] 2
----- file_2_data[3] 3
----- file_2_data[4] 4
----- file_2_data[5] 5
----- file_2_data[6] 6
----- file_2_data[7] 7
----- file_2_data[8] 8
----- file_2_data[9] 9
----- file_2_data[10] 10

 
If you want to look up the data by line numbers then you need to index the original array by line numbers, i.e. use file_1_data[FNR]=$0 insetad of file_1_data[$1]=$0.

Also, consider using a for loop to print:

Code:
    for (i=1; i<=10; i++) {
        print "-----   file_1_data["i"] "  file_1_data[i]
    }

Annihilannic.
 
Alternativly:
Code:
awk '
    FILENAME==ARGV[1] { file_1_data[$1]=$0; print "----- 1 Line " NR " -----" $1; next }
    FILENAME==ARGV[2] { file_2_data[$1]=$0; print "----- 2 Line " NR " -----" $1; next }
    END {
        # do actual data processing here
        print "----- Line " NR " -----"
        for(i in file_1_data)
          print "-----   file_1_data["i"] "  file_1_data[i]
        for(i in file_1_data)
          print "-----   file_2_data["i"] "  file_2_data[i]
        print "\n"
    }
' infile1 infile2

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks PHV!
You always have the answers.
 
OOps, sorry for the typo in the second for() ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top