Awk multiple variable array: comparison


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk multiple variable array: comparison
# 1  
Old 04-30-2010
Question Awk multiple variable array: comparison

Foo.txt
Code:
20    40    57    50    22
51    66    26    17    15
63    18    80    46    78
99    87    2    14    14
51    47    49    100    58

Bar.txt
Code:
20 22
51 15
63 78
99 55
51 58

How to get output using awk
Code:
20 22 57 50
51 15 26 17
63 78 80 46
99 55 - -
51 58 49 100

Thanks
~GH
# 2  
Old 04-30-2010
Code:
nawk 'FNR==NR {foo[$1,$NF]=$3 OFS $4;next}{idx=$1 SUBSEP $2; print $0 OFS ((idx in foo)? foo[idx] : "-" OFS "-")}' Foo.txt Bar.txt

# 3  
Old 04-30-2010
Another way :
Code:
join Foo.txt Bar.txt |
awk '
{
  if ($NF == $(NF-1)) {
     print $1, $NF, $3, $4;
  } else {
     print $1, $NF, "-", "-";
  }
}
'

Jean-Pierre.
# 4  
Old 04-30-2010
without awk
Code:
join Foo.txt Bar.txt | while read A B C D E F
do
    echo -n "$A $F "
    [ $E = $F ] && echo "$C $D" || echo  "- -"
done

# 5  
Old 04-30-2010
so many cats - so little time! Smilie
# 6  
Old 05-01-2010
Thank you vgersh, aigles and frans.
Explanation of the code would be very much appreciated.
Code:
nawk 'FNR==NR {foo[$1,$NF]=$3 OFS $4;next}{idx=$1 SUBSEP $2; print $0 OFS ((idx in foo)? foo[idx] : "-" OFS "-")}' Foo.txt Bar.txt

~GH

Last edited by genehunter; 05-01-2010 at 04:36 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk comparison using multiple files

Hi, I have 2 files, I need to use column of file1 and do a comparison on file2 column 1 and print the mismatch is file3 as mentioned below. Kindly consider that file 1 is having uniq key(column) whereas in file2 we have multiple duplicates (like 44). These duplicates should not come in... (2 Replies)
Discussion started by: grv
2 Replies

2. Shell Programming and Scripting

awk if comparison with variable

Hi , Need help, p for value in `awk -F, '{print $1 }' ad | uniq` do x=$(echo $value) echo $x echo `awk -F, '{if( $1 == $x) sum = sum + $8 } END{print sum}' ad` --- not working echo `awk -F, '{if($1 == “MT”) sum = sum + $8 } END{print sum}' ad` -- Working but hard coded done; ad... (4 Replies)
Discussion started by: nadeemrafikhan
4 Replies

3. Shell Programming and Scripting

storing multiple values in a array variable

Am using a find command in my script .The output may be one or more. I need to store those values in a array and need to access those. Am unable to find the solution . Any help on this will be helpful. if < code> else a=<find command output which gives the file name either 1 or more> if 1... (1 Reply)
Discussion started by: rogerben
1 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

Strange variable comparison result in awk

So, I'm making a little awk script that generates a range-based histogram of a set of numbers. I've stumbled onto a strange thing. Toward the end of the process, I have this test: if ( bindex < s ) "bindex" is the "index" of my "bin" (the array element that gets incremented whenever a... (2 Replies)
Discussion started by: treesloth
2 Replies

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

7. Shell Programming and Scripting

awk comparison with variable

hi, I want to compare i variable in the awk statement but its not working out. Pl help me out If we do the comparison like this its OK, cat sample | awk -F" ", '{if ($1=="1-Sep-2009") print $1,$2,$3,$4,$5}' But if u use a variable instead of "1-Sept-2009", it does not return anything,... (2 Replies)
Discussion started by: asadlone
2 Replies

8. UNIX for Dummies Questions & Answers

multiple comparison in awk

I have an input file. Each line in it has several characters. If the first three characters of the line is '000' or '001' or '002' or '003', I need to print it in output. How can I do this in awk. I am able to do if the search string is only one (let us say 000). cat <filename> | awk... (1 Reply)
Discussion started by: paruthiveeran
1 Replies

9. Shell Programming and Scripting

How to store query multiple result in shell script variable(Array)

:) Suppose,I have one table A. Table A have one column. Table A have 10 rows. I want this 10 rows store into shell script variable. like #!/bin/ksh v_shell_var=Hi here in call oracle , through loop How can I store table A's 10 rows into v_shell_var (Shell Script Array). Regards, Div (4 Replies)
Discussion started by: div_Neev
4 Replies

10. Shell Programming and Scripting

AWK program with array variable

Hi, I made a small awk program just to test array variables. I couldn't find anything wrong with it. But it doesn't give out valid numbers.( just 0.00 ) Do you see any problem that I didn't see? Thanks in advance! Here is the program: ################################## BEGIN { FS =... (4 Replies)
Discussion started by: whatisthis
4 Replies
Login or Register to Ask a Question