LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-20-2010, 07:03 AM   #1
apanimesh061
Member
 
Registered: Sep 2010
Posts: 51

Rep: Reputation: 0
Exclamation Help in using 'awk' programming........


I have a text file with 4 columns and 18 rows. All are integers.
I wish to find the average of first 12 numbers in the third column using 'awk'.

Please help !!!!
ASAP
Thanks......
 
Old 11-20-2010, 07:38 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Sure. Where are you getting stuck? It appears a fairly trivial problem that would easily be solved with some searching.
 
Old 11-20-2010, 09:14 AM   #3
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Yup, it has been asked before. ASAP = homework due tomorrow ?

Here's an awk reference:
http://www.grymoire.com/Unix/Awk.html
 
Old 11-21-2010, 06:23 AM   #4
apanimesh061
Member
 
Registered: Sep 2010
Posts: 51

Original Poster
Rep: Reputation: 0
@ H TEXMEX H.....
I checked out the link.......I was good.....helped me a lot......
Running awk commands in the terminal was easy to some extent. The main problem arising is in Shell code.

Code:
#!/bin/awk -f
BEGIN 
{

    total=0;
}

{
   total+=$3;
}
END 
{

    print lines " lines read";
    print "total is ", total;
    if (lines > 0 ) {
	print "average is ", total/NR;
    } else {
	print "average is 0";
    }
}
This shell is correct...but at the terminal how will I execute it ?????
I also wanted to know the use of '#! /bin/awk -f'
Please help...!!!!!
 
Old 11-21-2010, 06:34 AM   #5
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Well, here's something that might work:

Code:
awk '{lines++; if (lines <= 12) {sum+=$3}; if (lines == 12) print sum/12}' test.txt
Note that variables are always initialized in awk, so you don't actually have to.
 
Old 11-21-2010, 07:30 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Or as you are already using NR just use it from the start:
Code:
 awk 'NR<=12{total+=$3}NR==12{print total/NR}' file
 
Old 11-21-2010, 07:45 AM   #7
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by apanimesh061 View Post
I also wanted to know the use of '#! /bin/awk -f'
Please help...!!!!!
http://en.wikipedia.org/wiki/Shebang_line
 
Old 11-21-2010, 07:57 AM   #8
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Quote:
Originally Posted by grail View Post
Or as you are already using NR just use it from the start:
Code:
 awk 'NR<=12{total+=$3}NR==12{print total/NR}' file
You're right, this is better. However, I still like if statements to make it more readable.
 
Old 11-21-2010, 09:06 AM   #9
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
I do recall that there was a similiar problem a couple of days ago. Using NR is problematic if the file contains blank lines. So to deal with blank lines:
Code:
awk '($3~/[0-9]+/ && c<3){t+=$3;c++}END{print t/c}' file
As for your Shebang issue:
On my system awk is located in /usr/bin/. So you might also have to change your Shebang to '#!/usr/bin/awk -f'.
However, I see a windows logo in your posts. Are you running awk from windows? I am not sure if windows handles something like Shebangs. I *think* it doesn't.

In any case here is some more info on Shebangs:
http://en.wikipedia.org/wiki/Shebang_%28Unix%29
http://wiki.linuxquestions.org/wiki/Shebang
http://tldp.org/LDP/abs/html/sha-bang.html
 
  


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
Using wildcard in awk programming, how ? PT 2 bfoschia Programming 2 01-10-2007 02:35 PM
Using wildcard in awk programming, how ? sarajevo Programming 2 10-18-2006 07:06 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:46 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