Sum value using sed or awk ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sum value using sed or awk ?
# 1  
Old 05-01-2015
Sum value using sed or awk ?

Hello all,

how would one go about writing a command using sed/awk that will give me an output that can sum up the number of time each user has done something and also add the amount of time...

so output would be for example
"
Code:
smiths has run 3 process and for time taken of value: 224

"

here is the sample data (username | Process name | Time taken)...

Code:
smiths|Login|2
olivert|Login|2
northj|Login|2
denniss|Login|2
smithd|Login|2
smiths|Time Reporting|210
olivert|Payroll|155
northj|Server Maintenance|70
northj|Logfile Reporting|45
denniss|Report Printing|80
smithd|Payroll Program Updates|150
smithd|Benefit Program Updates|180
smiths|Logout|2
olivert|Logout|2
northj|Logout|2
denniss|Logout|2
smithd|Logout|2


Last edited by Scrutinizer; 05-01-2015 at 02:34 PM.. Reason: code tags
# 2  
Old 05-01-2015
sed is not a programming language. It can't do math.

awk is a programming language, and can.

Is this homework?
# 3  
Old 05-01-2015
Lol no this is not home work...

we are being provided with dump file likes this by the DBA's from a different company and i been asked to come up with a way to minuplate this dump file to spit some output...
# 4  
Old 05-01-2015
Try:
Code:
awk -F\| '{T[$1]+=$3; C[$1]++} END{for(i in T) print i, C[i], T[i]}' file

If you count login and logout as well..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 05-01-2015
Quote:
Originally Posted by Scrutinizer
Try:
Code:
awk -F\| '{T[$1]+=$3; C[$1]++} END{for(i in T) print i, C[i], T[i]}' file

If you count login and logout as well..
awesome that seem to work but how do i get it down to a specfic user/output...
# 6  
Old 05-01-2015
Try:
Code:
awk -F\| '{T[$1]+=$3; C[$1]++} END{for(i in T) printf "%s has run %s process and for time taken of value: %s\n", i, C[i], T[i]}' file

This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk and sum

This is my file vol0 285GB vol0.snapshot 15GB vol11_root 0GB vol12_root 47GB vol12_root.snapshot 2GB I need the output vol0 285GB,vol0.snapshot 15GB,sum-300GB vol11_root 0GB,nosnap,sum-0Gb vol12_root 47GB,vol12_root.snapshot 2GB,49GB I was trying to use paste -d, --. But... (9 Replies)
Discussion started by: ranjancom2000
9 Replies

2. Shell Programming and Scripting

awk and or sed command to sum the value in repeating tags in a XML

I have a XML in which <Amt Ccy="EUR">3.1</Amt> tag repeats. This is under another tag <Main>. I need to sum all the values of <Amt Ccy=""> (Ccy may vary) coming under <Main> using awk and or sed command. can some help? Sample looks like below <root> <Main> ... (6 Replies)
Discussion started by: bk_12345
6 Replies

3. Shell Programming and Scripting

sum from ls -al |awk '{print $5}'

// AIX 5.3 & AIX 6.1 ls -al |awk '{print $5}' This gives each file's size in byte. I need to get: - the sum of all files in Giga bytes with loop. - excluding the size of directories (ls -al returns the size of directories). There are hundreds and thousands of files, so summing up... (8 Replies)
Discussion started by: Daniel Gate
8 Replies

4. Shell Programming and Scripting

Summing over specific lines and replacing the lines with the sum using sed, awk

Hi friends, This is sed & awk type question. I have a text file which has numbers spread all over the file. I want to sum the series of numbers whenever i find it and produce an output file with the sum. For example ###start of input text file #### abc def ghi 1 2 3 4 kjld random... (3 Replies)
Discussion started by: kaaliakahn
3 Replies

5. Shell Programming and Scripting

awk and count sum ?

I have a input.txt file which have 3 fields separate by a comma place, os and timediff in seconds tampa,win7, 2575 tampa,win7, 157619 tampa,win7, 3352 dallas,vista,604799 greenbay,winxp, 14400 greenbay,win7 , 518400 san jose,winxp, 228121 san jose,winxp, 70853 san jose,winxp, 193514... (5 Replies)
Discussion started by: sabercats
5 Replies

6. UNIX for Dummies Questions & Answers

awk question about sum

Hi everyone, i need help with a simple task. I have a file withe the format: "01/20/2012 23:10:13.979","49","49","48","19" "01/20/2012 23:15:13.969","47","47","48","18" "01/20/2012 23:20:13.975","47","47","45","17" "01/20/2012 23:25:13.980","44","44","44","17" "01/20/2012... (3 Replies)
Discussion started by: civilianwarfare
3 Replies

7. Shell Programming and Scripting

Sum using awk

Hi all, I need to sum values for fields in a delimited file as below: 2010-03-05||| 2010-03-05|||123 2010-03-05|467.621|369.532| 2010-03-06||| 2010-03-06||2| 2010-03-06|||444 2010-03-07||| 2010-03-07||| 2010-03-07|655.456|1019.301| Code used is: nawk -F "|" ' { sum +=... (7 Replies)
Discussion started by: Katabatic
7 Replies

8. Shell Programming and Scripting

scripting/awk help : awk sum output is not comming in regular format. Pls advise.

Hi Experts, I am adding a column of numbers with awk , however not getting correct output: # awk '{sum+=$1} END {print sum}' datafile 2.15291e+06 How can I getthe output like : 2152910 Thank you.. # awk '{sum+=$1} END {print sum}' datafile 2.15079e+06 (3 Replies)
Discussion started by: rveri
3 Replies

9. Shell Programming and Scripting

Sum with awk

Hi,consider this fields, $1 $2 $3 981 0 1 984 0 4 985 1 0 987 0 2 990 0 0 993 0 3 995 2 0 996 0 1 999 0 4 for each occurence of zero in column $2 and $3 I need to sum $1 fields, so for example, in this piece of code the result of $1 is 8910. I'm sure... (2 Replies)
Discussion started by: cv313x
2 Replies

10. Shell Programming and Scripting

help sum columns by break in first column with awk or sed or something.

I have some data that is something like this? item: onhand counted location ITEM0001 1 0 a1 ITEM0001 0 1 a2 ITEM0002 5 0 b5 ITEM0002 0 6 c1 I want to sum up... (6 Replies)
Discussion started by: syadnom
6 Replies
Login or Register to Ask a Question