LinuxQuestions.org
Help answer threads with 0 replies.
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, 06: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, 07: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, 09:53 PM   #3
incomingid
LQ Newbie
 
Registered: Oct 2008
Posts: 15

Original Poster
Rep: Reputation: 0
Thanks alot.
 
Old 04-01-2009, 10: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, 10: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, 11:44 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


All times are GMT -5. The time now is 04:04 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration