LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-01-2009, 05:15 PM   #1
incomingid
LQ Newbie
 
Registered: Oct 2008
Posts: 15

Rep: Reputation: 0
About awk command


I am trying to add row $2 to find the total.

Ex.
File.txt
Age 21
Age 31
Age 13
Age 20

I want to use awk command to get the age total ( in this case it would be 85)
 
Old 04-01-2009, 06:09 PM   #2
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
There's a number of ways to handle this, but what I would do is:
Code:
$ awk '{ sum = sum + $2 } END{ print sum}' data-file
There's a free online gawk book that you should read if you don't understand what that is doing. (It's a good reference to keep bookmarked as well.)
 
Old 04-01-2009, 08:53 PM   #3
incomingid
LQ Newbie
 
Registered: Oct 2008
Posts: 15

Original Poster
Rep: Reputation: 0
Thanks alot.
 
Old 04-01-2009, 09:06 PM   #4
incomingid
LQ Newbie
 
Registered: Oct 2008
Posts: 15

Original Poster
Rep: Reputation: 0
I tried your command and i get the following error:

awk: {total = total + $2) END{ print sum}
awk: ^ syntax error
awk: cmd. line:1: {total = total + $2) END{ print sum}
awk: cmd. line:1: ^ unexpected newline or end of string
 
Old 04-01-2009, 09:10 PM   #5
incomingid
LQ Newbie
 
Registered: Oct 2008
Posts: 15

Original Poster
Rep: Reputation: 0
Sorry, i found the error there, but the output of that command is 0.
 
Old 04-01-2009, 10:44 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Code:
awk '{total+=$2}END{print total}' file
works here ...
Maybe your file looks different from what you posted?
 
  


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
help reg. awk command phone_book Linux - Newbie 1 03-20-2009 09:50 PM
awk command toaravind Linux - General 5 07-02-2008 01:25 AM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM
the 'awk' command. iconicmoronic Linux - Newbie 2 04-08-2007 12:29 AM
cut / awk command?? Sammy2ooo Linux - Newbie 1 05-27-2003 05:46 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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