LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-16-2007, 12:33 AM   #1
uttam_h
LQ Newbie
 
Registered: Sep 2002
Location: mangalore,india
Distribution: redhat
Posts: 28

Rep: Reputation: 15
awk help


hi all,
i have a log file with following content

<req>
<op>login</op>
<succ>0</succ>
</req>
<req>
<op>login</op>
<succ>1</succ>
</req>
<req>
<op>logout</op>
<succ>0</succ>
</req>
<req>
<op>delete</op>
<succ>0</succ>
</req>
<req>
<op>message</op>
<succ>0</succ>
</req>
..............
.............
...............
..............

i want to count the op parameters(eg. login, logout....) for which succ is 0 using awk

Regards,
uttam hoode
 
Old 10-16-2007, 01:38 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
awk '{ last=line;line=$0;}
    /<succ>0<\/succ>/ { gsub(/<op>|<\/op>/,"",last) ; store[last]++} 
    END{ for(i in store) print store[i],i}' "file"
 
Old 10-16-2007, 02:47 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: NetBSD, Void, Debian, Mint, Ubuntu, Puppy, Raspbian
Posts: 3,485

Rep: Reputation: 223Reputation: 223Reputation: 223
grep -c '<succ>0</succ>'
 
Old 10-16-2007, 03:40 AM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914
Quote:
Originally Posted by bigearsbilly View Post
grep -c '<succ>0</succ>'
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.



Cheers,
Tink

Last edited by Tinkster; 10-16-2007 at 03:41 AM.
 
Old 10-16-2007, 03:53 AM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914
And a slightly different approach...
Code:
awk 'BEGIN{RS="/req>";FS="\n"}/succ>0/{if($0~/op>login/){login++};if($0~/op>logout/){logout++}} END{print login" "logout}' log


Cheers,
Tink
 
Old 10-16-2007, 04:22 AM   #6
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 37
... and another one:

Code:
awk '$2=="op"{k=$3;x[k];next}
$2=="succ"&&$3==0{x[k]++}END{
for(i in x)print i,x[i]+0}' FS="[<>]" file
 
Old 10-16-2007, 04:42 AM   #7
uttam_h
LQ Newbie
 
Registered: Sep 2002
Location: mangalore,india
Distribution: redhat
Posts: 28

Original Poster
Rep: Reputation: 15
hi all,
Thanks for the solutions.
Regards,
uttam hoode
 
Old 10-16-2007, 07:23 AM   #8
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 902

Rep: Reputation: 210Reputation: 210Reputation: 210
If ( "<succ>0</succ>" )
<fail>1</fail> ;

OP <Quote>i want to count the op parameters(eg. login, logout....) for which succ is 0 using awk</Unquote>
 
Old 10-16-2007, 08:08 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: NetBSD, Void, Debian, Mint, Ubuntu, Puppy, Raspbian
Posts: 3,485

Rep: Reputation: 223Reputation: 223Reputation: 223
oops, sorry tink

is this understandable?

Code:
#!/usr/bin/perl

local $/ = undef; # slurp mode

@L = split /<req>/, <ARGV>;
@L = grep /login/, @L;
@L = grep />0</, @L;

print scalar @L, "\n";
#   print "@L";
 
Old 10-16-2007, 10:32 AM   #10
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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; }
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
awk help please stefaandk Programming 6 10-02-2007 07:50 AM
Awk opensource82 Linux - General 5 09-19-2007 09:08 AM
Help with awk kshkid Programming 1 12-22-2006 05:59 AM
using awk meniscus Linux - Newbie 6 10-05-2006 12:39 PM
using awk wedgeworth Linux - Newbie 9 02-20-2004 07:48 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration