Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6914829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:23:53+00:00 2026-05-27T09:23:53+00:00

I am writing a awk script that takes some columns of input in a

  • 0

I am writing a awk script that takes some columns of input in a text file and print out the largest value in each column

Input:

 $cat numbers
    10      20      30.3    40.5
    20      30      45.7    66.1
    40      75      107.2   55.6
    50      20      30.3    40.5
    60      30      45.O    66.1
    70      1134.7  50      70
    80      75      107.2   55.6

Output:

80  1134.7  107.2       70

Script:

BEGIN {
val=0;
line=1;
}
{
if( $2 > $3 )
{
   if( $2 > val )
   {
      val=$2;
      line=$0;
   }
}
else
{
   if( $3 > val )
   {
      val=$3;
      line=$0;
   }
}
}
END{
print line
}

Current output:

 60 30  45.O    66.1

What am I doing wrong first awk script

=======SOLUTION======

 END {
  for (i = 0; ++i <= NF;)
   printf "%s", (m[i] (i < NF ? FS : RS))
   }
 {
 for (i = 0; ++i <= NF;)
   $i > m[i] && m[i] = $i
 }

Thanks for the help

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:23:53+00:00Added an answer on May 27, 2026 at 9:23 am

    You can create one user defined function and then pass individual column arrays to it to retrieve the max value. Something like this –

    [jaypal:~/Temp] cat numbers
    10 20 30.3 40.5
    20 30 45.7 66.1
    40 75 107.2 55.6
    50 20 30.3 40.5
    60 30 45.O 66.1
    70 1134.7 50.0 70
    80 75 107.2 55.6
    
    [jaypal:~/Temp] awk '             
    function max(x){i=0;for(val in x){if(i<=x[val]){i=x[val];}}return i;} 
    {a[$1]=$1;b[$2]=$2;c[$3]=$3;d[$4]=$4;next} 
    END{col1=max(a);col2=max(b);col3=max(c);col4=max(d);print col1,col2,col3,col4}' numbers
    80 1134.7 107.2 70
    

    or

    awk 'a<$1{a=$1}b<$2{b=$2}c<$3{c=$3}d<$4{d=$4} END{print a,b,c,d}' numbers
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a shell script that outputs all untranslated strings from gettext .po file,
I am writing a bash script that modifies a file that looks like this:
I am writing an awk script that will take the output of grep and
I'm writing a shell script that opens a file and needs to find a
Writing a shell script that pretty much wraps around an Awk script. I'd like
Writing a JSP page, what exactly does the <c:out> do? I've noticed that the
Writing documentation in html requires some code examples. What to do with characters that
at the moment I'm writing a simple script in ksh who should take some
I want to select some columns in a file and run some command on
I writing a ksh shell script and I have long comma separated string that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.