Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-26-2012
Registered User
 
Join Date: Mar 2012
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Awk to add selected row column data

Looks at the most efficient way to add up the column of data based off of the rows.

Random data
Name-Number-ID
Sarah-2.0-15
Bob-6.3-15
Sally-1.0-10
James-1.0-10
Scotty-10.7-15

So I would select all those who have ID = 15 and then add up total number

Code:
read - p "Enter ID number" Num

cut -d "-" -f 2-3 file.txt > temp.txt
awk -F "-" '/' $Num '/ {hour += $2} END {Print "Total is: " %.2f\n, number}' temp.txt

Not sure on the syntax or if hour+= should be $2 or $3
Sponsored Links
    #2  
Old 03-26-2012
...@...
 
Join Date: Feb 2004
Location: NM
Posts: 9,650
Thanks: 164
Thanked 642 Times in 619 Posts

Code:
awk -F '-'  '$3==15 {sum+=$2}  END{ print sum}'  randomfile

Sponsored Links
    #3  
Old 03-26-2012
Registered User
 
Join Date: Mar 2012
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by jim mcnamara View Post
Code:
awk -F '-'  '$3==15 {sum+=$2}  END{ print sum}'  randomfile


Code:
cut -d ":" -f 2-3 works_on > temp.txt
awk -F ':'  '$3==$var3 {sum+=$2}  END{ print "Total : sum}' temp.txt

since 15 can change then it would have to be a variable. $3 is the 3rd column correct?

I get the error
"awk: line 1: runaway string constant"
    #4  
Old 03-27-2012
Scrutinizer's Avatar
Moderator
 
Join Date: Nov 2008
Location: Amsterdam
Posts: 7,345
Thanks: 144
Thanked 1,754 Times in 1,591 Posts
You can't have variable expansion inside single quotes. Try:


Code:
awk -F: '$3==v{sum+=$2} END{print "Total :" sum}' v="$var3" temp.txt

Your FS changed from "-" to ":" , correct?
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Extract data based on match against one column data from a long list data patrick87 Shell Programming and Scripting 12 11-17-2009 03:27 AM
Fetch selected data from webpage sunnydynamic15 Shell Programming and Scripting 1 10-30-2009 07:44 AM
sorting csv file based on column selected tententen Shell Programming and Scripting 4 07-05-2009 10:17 PM
flags to suppress column output, # of rows selected in db2 sql in UNIX jerardfjay Shell Programming and Scripting 1 11-02-2005 05:48 AM



All times are GMT -4. The time now is 10:51 AM.