Using awk to get the maximum of a value in two different ranges


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using awk to get the maximum of a value in two different ranges
# 1  
Old 09-06-2017
Question Using awk to get the maximum of a value in two different ranges

Dear Unix gurus,

I have sample data organised like this and containing 6 columns (with headers):
Code:
label c2 c3 c4 c5 c6

where c2 to c6 are numeric values in columns c2 to 6.

I am trying to create a simple output in a new file containing 3 columns:
Code:
label max(c2 c3) max(c4 c5 c6)

where max is the maximum numerical value of columns 2-3 and 4-6.

I have been trying to do it with awk but somehow don't manage to get the syntax right...

Many thanks for your time and help!

KS
# 2  
Old 09-06-2017
Code:
awk '
{
($(NF-1) > $(NF)) ? _ : ($(NF-1)=$(NF)) ; NF--;
($(NF-1) > $(NF)) ? _ : ($(NF-1)=$(NF)) ; NF--;
max2=$NF ; NF--;
($(NF-1) > $(NF)) ? _ : ($(NF-1)=$(NF)) ; NF--;
($(NF-1) > $(NF)) ? _ : ($(NF-1)=$(NF)) ; NF--;
$0=$0 OFS max2;
print $0;
}
' OFS="\t" infile


Last edited by rdrtx1; 09-06-2017 at 12:20 PM.. Reason: 7 columns. no header.
# 3  
Old 09-06-2017
Thank you very much for your fast reply!

I tried and it seems that it doesn't work on my data: I just get calculations at the end of the output that don't really seem to make sense.

Could you please detail the code so I can investigate? Thank you very much

---------- Post updated at 03:07 PM ---------- Previous update was at 02:57 PM ----------

I thought I would give a more concrete example of my problem.
My input file is as follows:

Code:
label1	0	0	0	0	0	1
label2	0	0	0	0	0	1
label3	0.2	0.2	0.9	0	0	0
label4	0	0	0.8	0.1	0	0
label5	0.1	0.1	0.1	0.15	0.1	0.1
label6	0.1	0.15	0.1	0.1	0.1	0.1

with column 1 being a header of the row, and columns 2 to 7 being numerical values.

The output I would like is:

Code:
label1	0	1
label2	0	1
label3	0.9	0
label4	0.8	0.1
label5	0.1	0.15
label6	0.15	0.1

with:
column 1 = column 1 from the input
column 2 = maximum value of columns 2 to 4 from the input
column 3 = maximum value of columns 5 to 7 from the input

Many thanks!!
# 4  
Old 09-06-2017
Thanks to the example things are clear now.
Code:
#!/bin/sh
awk '
  function maxcol(startcol,endcol){
    _maxcol=$startcol
    while (++startcol<=endcol) {
      if ($startcol>_maxcol) _maxcol=$startcol
    }
    return _maxcol
  }
  { print $1, maxcol(2,4), maxcol(5,7) }
' infile

This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 09-06-2017
Many thanks, it works perfectly!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk working inside specific pattern ranges

Hi, I have a text file, which I am trying to parse. File contents: BEG Id Job1 Id Stage1 1 EN Id Job2 Id Stage2 BEG Id2 Job3 Id Stage4 2 EN I have to process the data in this between every BEG and EN. so I am trying to restrict the range and inside every... (1 Reply)
Discussion started by: Kulasekar
1 Replies

2. UNIX for Dummies Questions & Answers

Using awk to find and use the maximum value in column of data

Dear Unix Gurus, I have a text file with multiple columns, for example, see sample.txt below 0 1 301 1 4 250 2 6 140 3 2 610 7 1 180I want to find the maximum in, say, column 3, normalise all the values to this maximum value (to 4 decimal places) and spit everything into a new... (2 Replies)
Discussion started by: tintin72
2 Replies

3. Shell Programming and Scripting

Extract minimum/maximum using awk

From the below table I want to print highest value and lowest value using awk script. aaa 55 66 96 77 ggg 22 96 77 23 ddd 74 58 18 3 kkk 45 89 47 92 zzz 34 58 89 92 Thanks, Green edit by bakunin: it sure is not news to you that you should use CODE-tags, no? And that we do not want such... (3 Replies)
Discussion started by: gwgreen1
3 Replies

4. UNIX for Dummies Questions & Answers

Finding maximum occurrence value using awk

Hi everyone, I'm a new member at the forum I mistakenly posted this elsewhere too. I have a file like this: field 2 values are either 0 or negative. file test4: 100815 -20 118125 0 143616 0 154488 0 154488 0 154488 -6 196492 -5 196492 -9 196492 -7 27332 0... (5 Replies)
Discussion started by: meet77
5 Replies

5. Answers to Frequently Asked Questions

Finding maximum occurrence value using awk

Hi everyone, I'm a new member at the forum I have a file like this: field 2 values are either 0 or negative. file test4: 100815 -20 118125 0 143616 0 154488 0 154488 0 154488 -6 196492 -5 196492 -9 196492 -7 27332 0 29397 0 I would like to print a... (1 Reply)
Discussion started by: meet77
1 Replies

6. Shell Programming and Scripting

Print minimum and maximum values using awk

Can I print the minimum and maximum values of values in first 4 columns ? input 3038669 3038743 3037800 3038400 m101c 3218627 3218709 3217600 3219800 m290 ............. output 3037800 3038743 m101c 3217600 3219800 m290 (2 Replies)
Discussion started by: quincyjones
2 Replies

7. Shell Programming and Scripting

Saving ranges using awk

Hi All, Is there a way to save a range in variable for later printing? for example write somthing like this: awk ' /pattern1/,/pattern2/{f=range} /pattern3/{print f} ' I don't know excatly what "range" could be but is there a way to do this? (8 Replies)
Discussion started by: ghoda2_10
8 Replies

8. Shell Programming and Scripting

Create ranges on a file with awk

Hello all, Let's say we have this file : $ cat inputfile 2 3 4 7 8 11 15 16 17 I'm trying to make a script with awk which gives as output the following : (4 Replies)
Discussion started by: rany1
4 Replies

9. UNIX for Dummies Questions & Answers

Awk ranges for selecting numbers

Hi, I am trying to use AWK to do some editing and formating of large tables of numbers and I am having trouble getting it to work. For brevities sake, I won't show the whole table, but I have a sample set of code: und$ awk '{($2+0) > 50;print $1}' temp 2000 147 2008 128 2002 100 1999 47... (2 Replies)
Discussion started by: ikerrin1@gmail.
2 Replies

10. Shell Programming and Scripting

awk to print mon and max values of ranges

HI all I'm trying to write an awk script to print the min and max value in a range(s) contained in another file - the range values are in $2 EG 114,7964,1,y,y,n 114,7965,1,y,y,n 114,7966,1,y,y,n 114,7967,1,y,y,n 114,7969,1,y,y,n 114,7970,1,y,y,n 114,7971,1,y,y,n 114,7972,1,y,y,n... (3 Replies)
Discussion started by: Mudshark
3 Replies
Login or Register to Ask a Question