Using perl to obtain stats instead of grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using perl to obtain stats instead of grep
# 8  
Old 04-22-2010
Not sure what i was doing their Turk, that does work well, long day !!!

I've got the extra figures i was after, is there a simple way i can sum up these temp values at the end so i can see total success pass purchase for the day and total errors? What i mean is, an overall count for the success column, and a count for the other column combined (2 & 3)


PASS success failed already_booked internal_server_error

EPASS011 27 4 0 0
EPASS009 11 0 0 0
EPASS07 102 3 0 0
EPASS012 40 1 2 14
EPASS078 1545 18 0 3
EPASS013 627 6 0 0
EPASS013 1456 12 0 0
EPASS013 1266 4 17 4


Many thanks guys Smilie

Jeffers
# 9  
Old 04-23-2010
Quote:
Originally Posted by durden_tyler
Nope, it was meant to be "//" operator (truth and definedness) and not the "||" operator (OR).

See what happens here:

Code:
$
$
$ perl -le '%x=qw(10 0 20 0); $k=20; printf("\nKey   = %5s\nValue = %5s\n\n", $k, $x{$k} || -9)'
 
Key   =    20
Value =    -9
 
$
$ # oops!, -9 is *not* the value of the key 20.
$ # 0 is the value of the key 20.
$ # Let's try again...
$
$ perl -le '%x=qw(10 0 20 0); $k=20; printf("\nKey   = %5s\nValue = %5s\n\n", $k, $x{$k} // -9)'
 
Key   =    20
Value =     0
 
$
$ # Now that's better !!
$

Smilie

tyler_durden
I didn't saw this operator before in any book of perl but I had tried it but it does not work see below:-

"my perl version is v5.6.1"

Code:
perl -le '%x=qw(10 0 20 0); $k=20; printf("\nKey   = %5s\nValue = %5s\n\n", $k, $x{$k} // -9);'

o/p:-
Search pattern not terminated at -e line 1.

Code:
perl -le '%x=qw(10 0 20 0); $k=20; printf("\nKey   = %5s\nValue = %5s\n\n", $k, $x{$k} || -9);'

o/p
Key   =    20
Value =    -9

# 10  
Old 04-23-2010
Quote:
Originally Posted by ahmad.diab
I didn't saw this operator before in any book of perl but I had tried it but it does not work see below:-

"my perl version is v5.6.1"
...
It's a new operator in version 5.10 and higher.

perl5100delta - search.cpan.org

tyler_durden
# 11  
Old 04-24-2010
Hi Jeffers,

So to sum the values in the columns, you can modify the final print statement a bit like so, or sum it up in the main processing loop:

Code:
foreach (sort keys %resultsHash) {
    $temp = $resultsHash{$_};
    print "$_ : @$temp\n";
    $sum1 += @$temp[0];
    $sum2 += @$temp[1] + @$temp[2];
}
print "Success [$sum1]\nFail [$sum2]\n";

Sample run:
Code:
eturk-linux 09:41:10 (~)> ./filter.pl data.dat
EPASS013 : 1 0 4
EPASS088 : 2 1 0
EPASSV09 : 1 2 0
Success [4]
Fail [7]
eturk-linux 09:41:15 (~)>

As a general unsolicited piece of advice, I recommend strongly that if you will be using more Perl in the future, you read some documentation of the language. The real deal is here:

Learn Perl - www.perl.org

I also like the Perl monks site for more advanced topics:

http://www.perlmonks.org/?node=Tutorials

There are a ton of resources out there for the inquisitive Perl person.

Cheers,
turk451
# 12  
Old 05-01-2010
Thanks Turk,

Spent the week reading the sites recommended, still find it confusing but have now massively improved the script to incorporate other searches, not sure if its the best practise but im getting somewhere, thanks for the helpful advise.

Jeffers
# 13  
Old 07-01-2010
Hi Turk,

Wondered if you could offer some advise on arrays within arrays.

I would like to amend the script so that it doesnt count duplicate entries, what im seeing is a large error count when in fact if i run a sort and pipe it to uniq it turns out to be only a few users, is their a way i can modify the script to just get uniq users by the mobile number? Do i need to create another array within this array?

thanks
# 14  
Old 07-06-2010
Hi Jeffers,

I would probably pre-filter the data using shell commands. One command you could use just to count the number of unique users would be:
Code:
cut -f 4 -d',' data.dat | sort | uniq | wc -l

if the data file just contains a lot of duplicated entries that you want to pre-filter (assuming they are exact duplicates), just skip the cut step:
Code:
sort data.dat | uniq > data.dat.new

Then you can just run the regular script on the cleaned-up data set.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help in solving to obtain desired print output using awk or perl or any commands, Please help!!

I have an file which have data in lines as follows ad, findline=24,an=54,ab=34,av=64,ab=7989,ab65=34,aj=323,ay=34,au=545,ad=5545 ab,abc,an10=23,an2=24,an31=32,findline=00,an33=23,an32=26,an40=45,ac23=5,ac=87,al=76,ad=26... (3 Replies)
Discussion started by: deepKrish
3 Replies

2. Shell Programming and Scripting

How to grep a pattern in perl?

hello Everyone i am a newbie. i have a file which contains the following E:\gtmproj\script\i486_nt\obj\check_geomtools.exe: o:\portsrc\spg\system_1\i486_nt\advapps\TK-2\objmt\winclockmtq.lib E:\gtmproj\script\i486_nt\obj\check_geomtools.exe:... (12 Replies)
Discussion started by: Rashid Khan
12 Replies

3. Shell Programming and Scripting

Grep in PERL

Hi, Can anybody let me know how this grep will work. The input and output is not known. Also can you give me the details of any link where i can find clearly about grep Thanks in advance (1 Reply)
Discussion started by: irudayaraj
1 Replies

4. Shell Programming and Scripting

GREP Issue in Perl

Im storing multiple functions in a varaible called $check... The variable check contains the following: a() b() c() ... ..etc now im checking individually which function is kept in which file using GREP if ( grep \$check \i, <FILE> ) The problem is im getting the output for the... (1 Reply)
Discussion started by: rajkrishna89
1 Replies

5. Shell Programming and Scripting

Perl + and Grep

Hi All i have this script that uses glob to look in /var/log/messages.* my @messagefiles = glob "/var/log/messages.*"; and the code that uses it is this grep { /NVRM: Xid/ } @messages) but this spits out this /var/log/messages-20111030:Oct 25 13:43:04 brent kernel: NVRM:... (10 Replies)
Discussion started by: ab52
10 Replies

6. Shell Programming and Scripting

grep in perl

Hello I want to grep a line from a file saved in some directory. Can anyone please correct the code below: #!/usr/bin/perl -w $file = "/home/output.txt" $grep_line = "closing zip for topic"; `grep $grep_line* $file`; (1 Reply)
Discussion started by: sureshcisco
1 Replies

7. Shell Programming and Scripting

How to execute Grep in Perl.

$ grep edge test_1 |sort|uniq >result.txt $more result.txt edge-a-pas01.com 10.12.10.11 edge-b-pas02.com 10.12.10.12 edge-c-pas03.com 10.12.10.50 edge-d-pas03.com 10.12.10.10 how do we execute the above grep command using perl? Thanks in advance. (3 Replies)
Discussion started by: sureshcisco
3 Replies

8. Shell Programming and Scripting

Perl grep

OK here's the situation: I have got these lines which I have got to parse. If the line contains a particular string and any element from a previously defined array I need to take that particular line and do some further processing. if ((grep(/$_/,$1)) && (grep($pattern,@myarr))) { #Do... (2 Replies)
Discussion started by: King Nothing
2 Replies

9. Shell Programming and Scripting

grep using Perl

I'm using perl to do a grep of each line in a vendor file and find its occurrences in a specific directory. Any values found is saved in @dir. .....(file opened, etc.) .... while ($line=<FILE>){ @dir = `grep $line * `; } It's the specific usage of the system grep that I'm having... (7 Replies)
Discussion started by: gavineq
7 Replies

10. Shell Programming and Scripting

Newbie to perl - Help with stats script

Hi, this is my first post so here goes..... I need help... I am trying to write a script to produce some stats based on a number of searches in a log file. Now i know how to do this using multiple variables which are really just greps, but I want a more efficent way of doing this as my poor... (1 Reply)
Discussion started by: ARwebble
1 Replies
Login or Register to Ask a Question