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!

If statement compare two file

Status
Not open for further replies.

toax

Programmer
Jun 23, 2011
5
DE
I am beginer in programing, I would like to compare column and instead of it, give "if statement" and produce second file.
File 1
5 A B
20 C D

file 2 :

6 1 1
22 2 4

and produce file 3 :
6 1 1 A B
22 2 4 C D

file 3 produce according comparison between column 1 at file 1 and 2 , if $1 < 10 , $1 +/- 1 and if $1 > 10, $1 +/- 2 :
6 1 1 A B
22 2 4 C D

Help please




 
A starting point:
Code:
awk '
NR==FNR{x=$2" "$3;a[$1-1]=a[$1]=a[$1+1]=x;if($1>10)a[$1-2]=a[$1+2]=x;next}
{print $0" "a[$1]}
' File1 File2 >File3

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Your code is working, but when I have different size (row and header) between file 1 and 2 ,it is not work properly. And I have last question, is it possible to compare two file but they have different header , row and column with awk. As an example :
File 1:
2 2 (header #1)
3 4 5
C D 10
1 2 22
3 3 (header #2)
1 C 5
A B 22

file 2 :
1 3 (header #1)
1 2 5
3 4 20
2 5 (header #2)
1 3 4

Based on comparison of header #1, #2 and column 3 at file 1 and 2 , if $3 < 10 , $3 +/- 1 and if $3 > 10, $3 +/- 2 :And desired output (file 3) :

1 2 (header #1)
1 2 3 4 6
3 4 C D 20
2 3 (header #2)
1 3 1 C 4

So far I do something like that :
awk '
NR==FNR {a[int($3+1)] = $0; next}
a[int($3+1)] {$0 = a[int($3+1)] " " $0; print $4,$5,$1,$2,$6}' file1 file2>file3

but this code only work for $3 < 10 , $3 +/- 1, and I have problem,when write if statement, Do you have idea, how to produce desired output file ?
Thanks in advance for your help

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top