Link to home
Start Free TrialLog in
Avatar of Talmash
TalmashFlag for Israel

asked on

smart grep for counting numbers

hi ,

I have this kind of text :

repeat (200) @(posedge `TXQ_BOARD.clk); // ` For

repeat (10) @(posedge `TXQ_BOARD.clk); // ` For

repeat (100) @(posedge `TXQ_BOARD.clk); // ` For

repeat (100) @(posedge `TXQ_BOARD.clk); // ` For

I have got meny lines like that , and I want to
summarize the numbers : 200 + 10 + 100 +100
within a script that get the text.file as an input
and outputs the sum .

Tal
Avatar of ozo
ozo
Flag of United States of America image

perl -ne '$sum+=$1 while/(\d+)/g;END{print "$sum\n"}' file.text
Avatar of yuzh
yuzh

Hi  Talmash,

    Here's a little script for you to do the job:

#===================================================
#!/usr/bin/sh
INFILE=your-input-file
num=0
sum=0
for num in `cat ${INFILE} | awk '{print $2}' | tr -d "()"`
do
     sum=`expr ${sum} + $num`
done

echo " The Sum is ${sum} \n"

#====================================================

    Cheers!

Regards
    yuzh
Hi Talmash

   I forgot to tell you that you need to grep the lines with numbers first,
then, use the script above to do your job.

   eg
                                                                                                         
                        #===================================================   #!/usr/bin/sh
 INFILE=your-input-file
 num=0
 sum=0
 for num in `cat ${INFILE} | awk '{print $2}' | grep "([1-9]" | tr -d "()"`
 do
 sum=`expr ${sum} + $num`
 done

 echo " The Sum is ${sum} \n"

                        #====================================================
Hi Talmash

   I forgot to tell you that you need to grep the lines with numbers first,
then, use the script above to do your job.

   eg
                                                                                                         
                        #===================================================   #!/usr/bin/sh
 INFILE=your-input-file
 num=0
 sum=0
 for num in `cat ${INFILE} | awk '{print $2}' | grep "([1-9]" | tr -d "()"`
 do
 sum=`expr ${sum} + $num`
 done

 echo " The Sum is ${sum} \n"

                        #====================================================
Avatar of Talmash

ASKER

ozo :

I got this error :

9:47{54}talm> perl -ne '$sum+=$1 while/(\d+)/g;END{print "$sum\n"}' in_file
syntax error in file /tmp/perl-ea18179 at line 1, next 2 tokens "END{"
syntax error in file /tmp/perl-ea18179 at line 2, next token "}"
Execution of /tmp/perl-ea18179 aborted due to compilation errors.

yuzh : I am quite limited using "for" function , but i'll try .
 
Talmash
What do you get from
perl -v
Avatar of Talmash

ASKER

10:07{69}talm> perl -v

This is perl, version 4.0

$RCSfile: perl.c,v $$Revision: 4.0.1.7 $$Date: 92/06/08 14:50:39 $
Patch level: 35

Copyright (c) 1989, 1990, 1991, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 4.0 source kit.
ASKER CERTIFIED SOLUTION
Avatar of yuzh
yuzh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
perl -ne '$sum+=$1 while/(\d+)/g;if(eof){print "$sum\n"}' file.text
Avatar of Talmash

ASKER

tnx yuzh .

ozo : your answer also accepted but yuzh was the 1st .

next time I'll be glad to give you the points .

tnx both to what I learned .

Talmash