ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
That'll count ALL successes, and not split them up
in login/logout, which is -if I understood the request
correctly - what the OP asked forl; it will count
messages and other ops, too.
Your input looks something like XML, although I think it needs to all be inside a single document tag to be valid. If you are parsing XML, it might be wise to use a proper parsing library - you never know if one day your input file will be reformatted like this:
Code:
<req>
<op>
login
</op>
<succ>
0
</succ>
</req>
...which will break the suggestions above. The rules of computing say this will happen at the worst possible time and cause you a lot of grief.
Parsing XML properly is a little more complicated that using the pattern based approaches above, but it is more reliable.
Code:
#!/usr/bin/perl
use warnings;
use strict;
use XML::XPath;
use XML::XPath::XMLParser;
# read in all files specified on command line, or data from standard input
# into the variable $xml_source. Since the input isn't valid (there can be
# only one base tag in an XML document), we will enclose the whole lot it
# in <doc> tags.
my $xml_source = "<doc>\n";
while (<>) { $xml_source .= $_; }
$xml_source .= "</doc>\n";
# This will be used to count occurrances of the various <op> sections.
my %counters;
# Now create a parser object which looks at $xml_source
my $xp = XML::XPath->new(xml => $xml_source);
# find all req/op tags
my $nodeset = $xp->find("/doc/req/op");
# for each op tag count the contents.
foreach my $node ($nodeset->get_nodelist) {
addcount($node->string_value);
}
# output the results.
foreach my $key (keys %counters) {
printf("%-3d %s\n", $counters{$key}, $key);
}
# Since we are using warnings, we cannot use an undefined value and assume
# it will be 0, so we whck and if it is already defined incremement it, else
# set the value to 1.
sub addcount {
my $key = shift || return;
if ( defined($counters{$key}) ) { $counters{$key}++; }
else { $counters{$key} = 1; }
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.