|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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.txtNot sure on the syntax or if hour+= should be $2 or $3 |
| Sponsored Links | ||
|
|
#2
|
|||
|
|||
|
Code:
awk -F '-' '$3==15 {sum+=$2} END{ print sum}' randomfile |
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
Quote:
Code:
cut -d ":" -f 2-3 works_on > temp.txt
awk -F ':' '$3==$var3 {sum+=$2} END{ print "Total : sum}' temp.txtsince 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
|
||||
|
||||
|
You can't have variable expansion inside single quotes. Try: Code:
awk -F: '$3==v{sum+=$2} END{print "Total :" sum}' v="$var3" temp.txtYour FS changed from "-" to ":" , correct? |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|