Consider following files:
file1
:
boo,8,1024
foo,7,2048
file2
:
foo,0,24,154
noo,0,10,561
file3
:
24,154,7,1024,0
What I need is to go to File1 and check if $2==7
; if true, take $1
, $2
and $3
from File1; now I have to compare if $1
from File1 equal to $1
from File2; if true, I have to take $3
and $4
from File2 which not exist in File1, then I have to go to File3 and check if $1
from File3 is equal to $3
from File2, and $2
from File3 is equal to $4
from File2; if yes, then I have to check if $2
from File1 is equal to $3
from File3, then if this condition is true, I have to compare $3
from File1 with $4
from File3, if $3
from File1 is more than $4
from File3.
I tried the following script:
cat [file1] [file2] [file3] |
awk -F,
'{if(NF==3)
{if($2==7){a[$1]=$1; b[$1]=$2; c[$1]=$3}
}else
{if(NF==4){if(a[$1]==$1){d[$3]=$3; e[$4]=$4}
}else
{if(NF==5){if(d[$1]==$1 && e[$2]==$2){print a[$1], b[$1], c[$1], d[$1]}}
}
}
}'
The desired output is:
foo,7,2048,24,154,1024
Best Answer
That worked for me:
Explanation:
FNR==1{++f}
) increments the file index to later determine in which file we are 1-3.$2
equals7
a1
with$1
as index anda2
with$2
as index and$3
as valueo
variable (output) with the first 3 fields$1
offile2
equals$1
offile1
(prevously written ina1
)$3
and$4
to the output variableo
.a3
with$3
as index and$4
as value.$1
equals file2s$3
(index ofa3
)$2
equals file2s$4
(value ofa3
)$3
equals file1s$2
(index ofa2
)$4
is lower than file1s$3
(value ofa2
)o
.