Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

share|improve this question
1  
is it homework? –  richard May 26 at 15:30

3 Answers 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..

share|improve this answer

With GNU awk

paste  file{1..3} | awk '{n=split($0, a, "\t");
           asort(a,b,"@val_str_desc"); print b[1]}'
share|improve this answer
paste file[123] |
while read -a values; do printf "%s\n" "${values[@]}" | sort | tail -1; done
share|improve this answer
    
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 at 13:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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