LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   find, print and count specific characters after string criteria (https://www.linuxquestions.org/questions/linux-newbie-8/find-print-and-count-specific-characters-after-string-criteria-878560/)

Alkass 05-03-2011 09:02 AM

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

Guttorm 05-03-2011 09:24 AM

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}'

Alkass 05-03-2011 09:36 AM

Hi!

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

Cheers


All times are GMT -5. The time now is 06:39 AM.