0

I have three files. All of them having only one column.

File1 
M
0
M
A

File2 
A
0
M
A

File3
M
0
A
A

compare each row in the three files, i.e, for first row (M,A,M) hence the maximum occurrence is M. The in the output file should contain M in the first row. likewise for other rows in the three files

Output file
M
0
M
A

can anybody tell the awk script for this....

3

Try something like:

awk '$1"">A[FNR]{A[FNR]=$1} END{for(i=1; i<=FNR; i++)print A[i]}' file*

$1"" is used to force $1 into string context, so that a string comparison is performed..

1

With GNU awk

paste  file{1..3} | awk '{n=split($0, a, "\t");
           asort(a,b,"@val_str_desc"); print b[1]}'
1
paste file[123] |
while read -a values; do printf "%s\n" "${values[@]}" | sort | tail -1; done
  • In hindsight, I'm not keen on this solution: the external sort will be invoked once per line, so the performance will be slower than the awk solutions. – glenn jackman May 27 '14 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.