Newbie Awk data sorting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Newbie Awk data sorting
# 1  
Old 05-04-2007
Error 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  
Old 05-04-2007
Please make a try. If you are not able to do we will help you.
# 3  
Old 05-04-2007
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  
Old 05-04-2007
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  
Old 05-04-2007
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  
Old 05-04-2007
(6) Do not post classroom or homework problems.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sorting data - awk or sort or both

data: C812F5C9B 0818053014 P S SYSPROC SOFTWARE PROGRAM ABNORMALLY TERMINATED C812F5C9B 0818054514 P S SYSPROC SOFTWARE PROGRAM ABNORMALLY TERMINATED C812F5C9B 0818060014 P S SYSPROC SOFTWARE PROGRAM ABNORMALLY TERMINATED C812F5C9B 0818061514 P S SYSPROC ... (3 Replies)
Discussion started by: SkySmart
3 Replies

2. Shell Programming and Scripting

Sorting the Data

My actual data looks like below i have given only format. i can't give exact data format of my requirement due to some reasons. I this set of data lines about 5000 I need to come up with information in below exact format of my data set : Line<space>Number1<space>"somedata":... (1 Reply)
Discussion started by: ckaramsetty
1 Replies

3. UNIX for Dummies Questions & Answers

Help with Data Sorting

Hi All, I have a long list made of 4 columns containing entries such as the following example: a b c d 0 0 0 0 1 2 1 2 2 5 3 4 3 8 4 6 4 10 9 8 5 15 8 10So the top row is the header and I need to arrange the data in a way as to... (11 Replies)
Discussion started by: pawannoel
11 Replies

4. UNIX for Dummies Questions & Answers

Sorting data

Hello guys. I need help figuring this one out. It's probably really easy. Thanks in advance! I have a file say for example containing this: Rice Food Carrots Food Beans Food Plates Kitchen Fork Kitchen Knives Kitchen I need: Food Rice, Carrots, Beans Kitchen Plates, Fork,... (7 Replies)
Discussion started by: visuelz
7 Replies

5. Shell Programming and Scripting

Sorting the data right justified with through awk.

Dear all, I have a inputfile like some what like this - 1000.98651 1000.96696 999.98904 991.66864 986.51829 986.49467 17.44122 16.74039 16.74021 10.92725 desired output 1000.98651 1000.96696 0999.98904 (4 Replies)
Discussion started by: admax
4 Replies

6. UNIX for Dummies Questions & Answers

Sorting data from a to z

Hi, Let's say I have these 3 columns; NGC1234 6 9 SL899 4 1 NGC1075 8 3 SL709 5 2 And I want to sort the data according to the first column (from a to z) like having them as: NGC1075 8 3 NGC1234 6 9 SL709 5 2 SL899 4 1 Can that be done... (2 Replies)
Discussion started by: cosmologist
2 Replies

7. Shell Programming and Scripting

PERL data - sorting

Hello, I have a page where multiple fields and their values are displayed. But I am able to sort only a few fields. When I looked into the issue, it is seen that the for each row of info , an unique id is generated and id.txt is generated and saved. Only those fields which are inside that id.txt... (3 Replies)
Discussion started by: eagercyber
3 Replies

8. UNIX for Dummies Questions & Answers

sorting data from who by IP

Hello. I have an RS/6000 running AIX 4 and I need to be able to see if there are any users that are logged on more than once from the same terminal so I can kick them off to make room for other terminals. 64 connections is the limit. Currently I am doing this: who | more and then manually... (11 Replies)
Discussion started by: raidzero
11 Replies

9. Shell Programming and Scripting

Sorting blocks of data

Hello all, Below is what I am trying to accomplish: I have a file that looks like this /* ----------------- xxxx.y_abcd_00000050 ----------------- */ jdghjghkla sadgsdags asdgsdgasd asdgsagasdg /* ----------------- xxxx.y_abcd_00000055 ----------------- */ sdgsdg sdgxcvzxcbv... (8 Replies)
Discussion started by: alfredo123
8 Replies
Login or Register to Ask a Question