2

I have many files which each one contain many arrays(like input 1 and 2, below). Each array has two element numbers in the second row. I need to use a logical criterion to choose the highest number for the first and second column of the second row and then to print the second number of the first row.

For example in the file input 1 presented below, I see that the highest number is 10.8 (for the first column of the second row), so I will print 6.1703(which is the number located in the first row of this array). And the other highest number is 10.1261 (for the second column of the second row) , so I will print 6.1932 (which is the number located in the first row of this array)

So, I need to ouput in this way for many array. Here, we present only two files and an output.

Input file1

1: 6.1703
 10.8 2.6

2: 6.1932
 7.01640 10.1261

3: 6.5664
 6.356 8.1

4: 8.0923
 1.41 7.6

Input file2

1: 7.2703
 3.8 8.6

2: 7.6892
 4.7 7.6576

3: 7.85874
 16.356 5.1

4: 9.8348
 4.1 3.6

Output

6.1703     6.1932
7.85874    7.2703
1
  • 1
    Is this homework? What have you tried?
    – iruvar
    Jun 6, 2016 at 12:25

1 Answer 1

2

With GNU awk you can define how arrays should be sorted with PROCINFO special array. This way the problem can be simplified:

awk 'BEGIN{RS='\n\n'}{a[$3]=$2; b[$4]=$2} \
     END{PROCINFO["sorted_in"]="@ind_num_asc"; \
     for(i in a) x=a[i]; for(i in b) y=b[i]; print x,y}' file1

Output:

6.1703 6.1932

To run this for all files just make a loop over all of them in shell or rearrange this awk snippet.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .