The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sort problem talashil Shell Programming and Scripting 4 04-11-2008 04:56 PM
sort -k Indalecio Shell Programming and Scripting 4 03-16-2007 04:00 AM
Sorting problem "sort -k 16,29 sample.txt > output.txt" ganapati Shell Programming and Scripting 3 08-01-2006 06:55 AM
SORT problem on SUN bobk544 UNIX for Dummies Questions & Answers 2 06-29-2006 04:46 PM
Problem with sort jyoung UNIX for Dummies Questions & Answers 1 04-13-2006 01:57 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-07-2008
sonicstage sonicstage is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 5
how to use awk to sort this problem out

there has several numbers which are:1,2,3,45,6,7,8,9,0,10,34,34,54,122,6756,54,87,99,2,1,45;

how to write a shell script orts the above numbers into descending order and puts them into and arrray and also find and prints the minimum and maximum of those numbers, and finds and prints the average of the numbers.

the outputs should be like:

minimum number:2
maximum number:10
average:5
  #2 (permalink)  
Old 04-07-2008
Yogesh Sawant's Avatar
Yogesh Sawant Yogesh Sawant is offline Forum Staff  
Part Time Moderator and Full Time Dad
  
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 1,086
is this homework?

what have you tried so far?
  #3 (permalink)  
Old 04-07-2008
sonicstage sonicstage is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 5
dun know how to start it

please teach me how to do it
  #4 (permalink)  
Old 04-07-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,088
why the max is 10 but not 6756 while the min is 1 but not 0.

If the requirement is what i assumed as above. Follow code can fullfill it.

Code:
filename:b

awk 'BEGIN{RS=","}
{
print 
}' b> c
sort -n c > c.t
rm c
sed '/^$/d' c.t > c
rm c.t
echo "min=`head -1 c`"
echo "max=`tail -1 c`"
echo "avg=`awk '{n=n+$0}END{print n/NR}' c`
  #5 (permalink)  
Old 04-07-2008
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,423
Another way with awk :
Code:
awk -v RS="," '
   NF && min=="" {
      min = max = $1 ;
   }
   NF {
     if ($1 > max) max = $1;
     if ($1 < min) min = $1;
     sum += $1
     cnt += 1
   }
   END {
      print "minimum number:" min;
      print "maximum number:" max;
      print "average:" sum/cnt;
   }
    ' inputfile
Jean-Pierre.
Closed Thread

Bookmarks

Tags
awk

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:25 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0