bash script, pattern matching + sum


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash script, pattern matching + sum
# 1  
Old 06-30-2008
Question bash script, pattern matching + sum

Hi guys, i have been struggling to achieve the following and therefor looking for some help.
I am trying to write something that will summerize the following output from my switches for daily totals.
Basicly if $1 $2 $3 doesnt change, we can keep adding up $4. Probably would use a awk print end statement but i cant get to work as it should. I tried inserting the data into arrays but that didnt work as well. Clearly im lacking the skills heh Smilie any form of help would be appreciated Smilie

Quote:
SW01 4/21 ALIGNMENT_ERROR 598
SW01 4/21 FCS_ERROR 598
SW01 4/21 IN_ERRORS 666
SW01 5/17 OUT_DISCARD 202392
SW01 5/17 OUT_DISCARD 203447
SW01 5/17 OUT_DISCARD 2585
SW01 5/17 OUT_DISCARD 320619
SW01 5/17 OUT_DISCARD 565
SW01 5/17 OUT_DISCARD 591978
SW01 5/17 OUT_DISCARD 727
SW01 5/17 OUT_DISCARD 967
SW01 5/33 OUT_DISCARD 200806
SW01 5/33 OUT_DISCARD 202417
SW01 5/33 OUT_DISCARD 2499
SW02 5/6 RUNTS 728
SW02 5/6 RUNTS 9103
SW02 2/27 ALIGNMENT_ERROR 598
Into the following output...
Quote:
SW01 4/21 ALIGNMENT_ERROR 598
SW01 4/21 FCS_ERROR 598
SW01 4/21 IN_ERRORS 666
SW01 5/17 OUT_DISCARD 40981240
SW01 5/33 OUT_DISCARD 23461087
SW02 5/6 RUNTS 2387
SW02 2/27 ALIGNMENT_ERROR 598
Thanks in advance
# 2  
Old 06-30-2008
This should summerize the fourth column based on the first 3 fields as key:

Code:
awk '{a[$1FS$2FS$3]+=$4}END{for(i in a){printf("%s %ld\n", i, a[i])}}' file

Regards
# 3  
Old 06-30-2008
Code:
$ awk '{ind=sprintf("%s %s %s",$1,$2,$3);s[ind]+=$4}END {for(i in s){print i,s[i]}}' h.in | sort
SW01 4/21 ALIGNMENT_ERROR 598
SW01 4/21 FCS_ERROR 598
SW01 4/21 IN_ERRORS 666
SW01 5/17 OUT_DISCARD 1323280
SW01 5/33 OUT_DISCARD 405722
SW02 2/27 ALIGNMENT_ERROR 598
SW02 5/6 RUNTS 9831

My output does not tally with yours, may i know how you get the number, it does seem to be the sum
# 4  
Old 06-30-2008
MySQL

Franklin52,
I'm very impressed! You gave me a oneliner doing exactly what i have been struggling with. Smilie Smilie


Chihung,
that is correct as mine was an example.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash pattern matching question

I need to check the condition of a variable before the script continues and it needs to match a specific pattern such as EPS-03-0 or PDF-02-1. The first part is a 3 or 4 letter string followed by a hyphen, then a 01,02 or 03 followed by a hyphen then a 0 or a 1. I know I could check for every... (4 Replies)
Discussion started by: stormcel
4 Replies

2. Shell Programming and Scripting

Non-greedy pattern matching in shell script

Hi all, Is Perl included by default in Ubuntu? I'm trying to write a program using as few languages as possible, and since I'm using a few Perl one-liners to do non-greedy matching, it's considered another language, and this is a bad thing. Basically, I'm using a Perl one-liner to grab XML... (3 Replies)
Discussion started by: Zel2008
3 Replies

3. Shell Programming and Scripting

Pattern Matching in Perl script

I have a big perl script need to fix a small pattern matching inside .. I have patterns like create unique index create index The pattern matching should look for both the pattern in the same statement, The existing matching looks for only "create unique index" The exising code for this i... (6 Replies)
Discussion started by: greenworld123
6 Replies

4. Shell Programming and Scripting

Sum of column matching pattern/string

Hi All, I have the following output file created as a result of one script called pattern_daily.log $ cat pattern_daily.log Approved|2|Wed, Oct 24, 2012 11:21:09 AM Declined|1|Wed, Oct 24, 2012 11:21:15 AM Approved|2|Wed, Oct 24, 2012 11:24:08 AM Declined|1|Wed, Oct 24, 2012 11:24:18 AM... (4 Replies)
Discussion started by: Gem_In_I
4 Replies

5. UNIX for Dummies Questions & Answers

Script to list non matching files using pattern

Hi, I am trying to write a script that list down all the files that do not match the pattern My pattern will be like this "*.jpg|*.xml|*.sql". This pattern will be stored in a file. The script need to read this pattern from the file and delete the files that does not match this pattern. It... (7 Replies)
Discussion started by: meenavin
7 Replies

6. Shell Programming and Scripting

Pattern matching in shell script

Hi, I am using following command to extract string from a file. String will be after last / (slash). awk -F\ / '{print $NF}' $FILE but while appending the output in file in script, it dosent work. File created but of zero size... can anyone please help `awk -F\\\/ '{print $NF}' $FILE` >... (3 Replies)
Discussion started by: Deei
3 Replies

7. Shell Programming and Scripting

shell script pattern matching

Hi, I need to create a shell script through which i need to populate email addresses in email columns of database table in mysql. Let say if email contains yahoo, hotmail, gtalk than email addresses need to move in their respective columns. # !/bin/sh yim="example@yahoo.com"... (3 Replies)
Discussion started by: mirfan
3 Replies

8. Shell Programming and Scripting

Pattern matching in a shell script?

I'm looking for a way to match a particular string in another string and if a match is found execute some command. I found the case statement can be used like this; case word in ) command ;; ] ... esac If my string to find is say "foo" in the string $mystring... (1 Reply)
Discussion started by: paulobrad
1 Replies

9. Shell Programming and Scripting

Pattern matching in BASH

i have 255 files in a directory named 000po.k thru 255po.k and I want to copy all files except 3: exclude 000po.k, 166po.k,168po.k I know the long way around it copying these files, but am looking for a shorter way of doing this: my old approach: # copy 001po.k thru 009po.k to target... (3 Replies)
Discussion started by: zoo591
3 Replies

10. UNIX for Dummies Questions & Answers

bash pattern matching echo *[! '/' ] doesn't work

without using ls, just using echo so purely pattern matching I can say echo */ <-- lists directories but how would I match files? surely something like *!/ or * but neither work ? it seems like there isn't much that I can put in but surely i should be able to put any ascii... (1 Reply)
Discussion started by: james hanley
1 Replies
Login or Register to Ask a Question