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
Sorting your data with msort iBot UNIX and Linux RSS News 0 05-19-2008 11:20 AM
Sorting data and place them in different folders Vinaykumar1 UNIX for Dummies Questions & Answers 29 05-14-2008 08:47 AM
sorting data using array in ksh ali560045 Shell Programming and Scripting 4 12-04-2007 04:26 AM
Sorting blocks of data alfredo123 Shell Programming and Scripting 8 07-05-2007 10:53 AM
sorting data based on multi columns sumeet UNIX for Advanced & Expert Users 2 02-15-2007 12: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 05-04-2007
i_am_a_robot i_am_a_robot is offline
Registered User
  
 

Join Date: May 2007
Posts: 5
Exclamation Newbie Awk data sorting

Hi guys, just started using awk here.

I've got a file called a.txt which contains a load of numbers on each line e.g.

35
232
654
1
9
4

I want to learn how to do three things:

1. Find the minimum
2. Find the maximum
3. Find the average

I want to learn how to do this using awk. Pretty basic stuff I should think but all I'm new to this so help please.
  #2 (permalink)  
Old 05-04-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Please make a try. If you are not able to do we will help you.
  #3 (permalink)  
Old 05-04-2007
i_am_a_robot i_am_a_robot is offline
Registered User
  
 

Join Date: May 2007
Posts: 5
well I've been trying to put it into an array using

split {$1, myArray, "\n"}

and

myArray{$1}

but it just throws out a load of errors.
  #4 (permalink)  
Old 05-04-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Quote:
Originally Posted by i_am_a_robot
well I've been trying to put it into an array using

split {$1, myArray, "\n"}

and

myArray{$1}

but it just throws out a load of errors.
Awk processes the file line by line. I suggest you to read any tutorials on awk before you proceed to solve the problems. You can search the forum for awk tutorial.
  #5 (permalink)  
Old 05-04-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,513
one way find min
Code:
awk 'NR==1 { temp = $1 ;next}
     { if ( $1 <= temp ) { temp=$1 }  }  
END{ print temp}' "file"
I leave you to do finding of max...

find avg
Code:
awk 'BEGIN{count=0}
     { count = count + $1} 
END{ print count/NR}' "file"
  #6 (permalink)  
Old 05-04-2007
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,209
(6) Do not post classroom or homework problems.
Closed Thread

Bookmarks

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 07:37 AM.


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