LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-27-2022, 04:55 PM   #1
Basher52
Member
 
Registered: Mar 2004
Location: .SE
Distribution: Arch
Posts: 401

Rep: Reputation: 22
awk, how to find maximum all columns value per row


I got an awk output showing columns and I need to know the highest value of these columns.
It's easy to do a max on per column on many rows but I need this on all columns per row.

Was thinking of creating an array that I later find the max value of but I can't get that array to work in the next 'pipe'

This is my result in columns and I need the highest value of this which is the best one screen can handle which is different per screen.

Code:
xrandr | awk -F "*" '/*/ { print $0}'
   1920x1080     60.00*+ 144.00   119.98   119.88    99.93    59.94    50.00
I need that 144 which is the frequency one screen can handle.
It's easy to cut out the resolution and the 'trash' but I the highest value that can differ between the screens so I don't know the column of it, thus my problem.

Hope my question is clear enough to understand


//B52
 
Old 10-27-2022, 07:27 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,129

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
Lose the "-F" - then you'll have access to the normal field number counter. Loop from 2 to NF and check each against current max for that record; computing 101. In awk can be simply done using the ternary operator. Then print $1 (probably) and max.
 
Old 10-28-2022, 08:48 AM   #3
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,602

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546

What are you trying to achieve here?

Are you sure it cannot be achieved directly with the xrandr command alone?

 
Old 10-28-2022, 01:07 PM   #4
Basher52
Member
 
Registered: Mar 2004
Location: .SE
Distribution: Arch
Posts: 401

Original Poster
Rep: Reputation: 22
Quote:
Originally Posted by boughtonp View Post
What are you trying to achieve here?

Are you sure it cannot be achieved directly with the xrandr command alone?
Have no clue but I think I got it. Did a loop through the columns and got what I wanted so, it's solved.
Here's the code:

Code:
awk '{max=$1;c=1;for(i=2;i<=NF;i++)if($i>max){c=i;max=$i} printf "%.0f in column:%s\n",max, c}' | aw    k '{print $1}'
I left the "in column..." in there even though I don't need it now, but I might later on...

Thx all for the suggestions.

btw, before this I cut out some 'rubbish' in the output, like column 1 and also the '*' and '+' sign.

Last edited by Basher52; 10-28-2022 at 01:09 PM.
 
Old 10-29-2022, 07:02 PM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,796

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Most awk versions stop processing trailing * + %
if a number is enforced.
A +0 is the trick
Code:
awk '{max=$1+0;c=1;for(i=2;i<=NF;i++)if($i+0>max){c=i;max=$i+0} printf "%.0f in column:%s\n",max, c}'

Last edited by MadeInGermany; 10-29-2022 at 07:05 PM.
 
Old 11-04-2022, 02:48 PM   #6
Basher52
Member
 
Registered: Mar 2004
Location: .SE
Distribution: Arch
Posts: 401

Original Poster
Rep: Reputation: 22
Quote:
Originally Posted by MadeInGermany View Post
Most awk versions stop processing trailing * + %
if a number is enforced.
A +0 is the trick
Code:
awk '{max=$1+0;c=1;for(i=2;i<=NF;i++)if($i+0>max){c=i;max=$i+0} printf "%.0f in column:%s\n",max, c}'
Tried this but it gave me 1920 thus part of the resolution not the highest frequency
 
Old 11-05-2022, 05:55 PM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,796

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Because it starts with column 1.
Let it start with column 2.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
transpose selected groups of rows of columns into multiple columns (arrays) with other columns retained to single row-group TempleStone4510! Linux - General 15 08-22-2022 06:52 PM
[SOLVED] Change row using awk based on column value akeka Programming 3 10-03-2012 02:43 AM
[SOLVED] AWK: add columns while keep format for other columns cristalp Programming 3 10-13-2011 06:14 AM
[SOLVED] find a null value in a row/column and delete entire row umix Linux - Newbie 10 10-13-2011 01:26 AM
difference between value *value and value * value PoleStar Linux - Newbie 1 11-26-2010 03:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration