LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-03-2011, 10:02 AM   #1
Alkass
Member
 
Registered: Mar 2010
Posts: 47

Rep: Reputation: 0
find, print and count specific characters after string criteria


Hello

I am having files like
Code:
<event>
 5   5  0.1862600E+00  0.8072931E+02  0.7957747E-01  0.1325616E+00
        2   -1    0    0  501    0  0.00000000000E+00  0.00000000000E+00  0.18415986606E+01  0.18415986606E+01  0.00000000000E+00 0. -1.
       -1   -1    0    0    0  501  0.00000000000E+00  0.00000000000E+00 -0.88472332593E+03  0.88472332593E+03  0.00000000000E+00 0.  1.
       24    2    1    2    0    0  0.00000000000E+00  0.00000000000E+00 -0.88288172727E+03  0.88656492459E+03  0.80729307988E+02 0.  0.
      -11    1    3    3    0    0  0.37598837948E+02  0.14320460394E+02 -0.47712304942E+03  0.47881640792E+03  0.00000000000E+00 0.  1.
       12    1    3    3    0    0 -0.37598837948E+02 -0.14320460394E+02 -0.40575867785E+03  0.40774851668E+03  0.00000000000E+00 0. -1.
#  0.8072931E+02  0.8072931E+02
</event>
<event>
 7   2  0.1862600E+00  0.8344825E+02  0.7957747E-01  0.1318551E+00
       -2   -1    0    0    0  501  0.00000000000E+00  0.00000000000E+00  0.14202019673E+02  0.14202019673E+02  0.00000000000E+00 0.  1.
       21   -1    0    0  501  502  0.00000000000E+00  0.00000000000E+00 -0.25404389242E+03  0.25404389242E+03  0.00000000000E+00 0. -1.
      -24    2    1    2    0    0 -0.45221119826E+01  0.21263379306E+01 -0.16281854930E+03  0.18222881188E+03  0.81685305131E+02 0.  0.
       11    1    3    3    0    0 -0.25317084797E+02 -0.17602112904E+01 -0.15865000036E+03  0.16066697153E+03  0.00000000000E+00 0. -1.
      -12    1    3    3    0    0  0.20794972815E+02  0.38865492210E+01 -0.41685489416E+01  0.21561840355E+02  0.00000000000E+00 0.  1.
       -1    1    1    2    0  503 -0.22452057292E+01  0.17414553699E+02 -0.33086791591E+02  0.37457221027E+02  0.00000000000E+00 0.  1.
       21    1    1    2  503  502  0.67673177119E+01 -0.19540891630E+02 -0.43936531858E+02  0.48559879184E+02  0.00000000000E+00 0. -1.
#  0.8344825E+02  0.8344825E+02  0.1755870E+02  0.1706226E+02
so, what I want to do, is to count how many times in total, (ie, reading every </event> block), from the first line I have the
"7" from the
Code:
7   2  0.1862600E+00  0.8344825E+02  0.7957747E-01  0.1318551E+00
or
"5" from a the line like
Code:
5   5  0.1862600E+00  0.8072931E+02  0.7957747E-01  0.1325616E+00
that means, I do only care about the first number one line after the <event>
I was using something like

Code:
#!/usr/bin/perl
        

        $/ = '</event>'; 

my $count=0;

        while (<>) { 

            $count++ if 
       
                    /
                        ^\s*        # Optional white space at start of line 
                       \ 6 
                        \s*$        # Optional white space at end of line 
                        /xms;   
                        }               

        print "counter - $count";
        print "";
but does not work... I think this also could be done simpler with grep + awk? (how?) - maybe also simple in bash ?

thanks in advance
 
Old 05-03-2011, 10:24 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,267

Rep: Reputation: 298Reputation: 298Reputation: 298
Hi

If I understand correctly, you want first word of all lines after lines containing "<event>" only. This outputs 5 and 7:

Code:
awk '/^<event>$/ { getline; print $1}'
 
1 members found this post helpful.
Old 05-03-2011, 10:36 AM   #3
Alkass
Member
 
Registered: Mar 2010
Posts: 47

Original Poster
Rep: Reputation: 0
Hi!

Thanks!!! This work nice, and from the output, I can do what I need

Cheers
 
  


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
i need to find a substring in a string and get the next 3 characters anurupr Linux - Newbie 8 03-07-2010 06:45 PM
Find/grep/wc command to find matching files, print filename and word count dbasch Linux - Newbie 10 09-14-2009 06:55 PM
help with grep... only print specific string with multiple matches? trey85stang Linux - General 2 05-11-2009 10:29 AM
characters count in a string powah Programming 3 07-19-2007 03:03 PM
find repeated characters in a string mcshen Programming 9 02-02-2004 06:43 PM


All times are GMT -5. The time now is 06:08 PM.

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