Solved

smart grep for counting numbers

Posted on 2001-06-24
10
236 Views
Last Modified: 2010-04-21
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
0
Comment
Question by:Talmash
  • 4
  • 3
  • 3
10 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 6222990
perl -ne '$sum+=$1 while/(\d+)/g;END{print "$sum\n"}' file.text
0
 
LVL 38

Expert Comment

by:yuzh
ID: 6223003
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
0
 
LVL 38

Expert Comment

by:yuzh
ID: 6223176
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"

                        #====================================================
0
 
LVL 38

Expert Comment

by:yuzh
ID: 6223179
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"

                        #====================================================
0
 
LVL 6

Author Comment

by:Talmash
ID: 6223526
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
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 84

Expert Comment

by:ozo
ID: 6223576
What do you get from
perl -v
0
 
LVL 6

Author Comment

by:Talmash
ID: 6223976
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.
0
 
LVL 38

Accepted Solution

by:
yuzh earned 50 total points
ID: 6224426
Hi Talmash,

   my script should work for you, just try it out.

Regards
   yuzh
0
 
LVL 84

Expert Comment

by:ozo
ID: 6225379
perl -ne '$sum+=$1 while/(\d+)/g;if(eof){print "$sum\n"}' file.text
0
 
LVL 6

Author Comment

by:Talmash
ID: 6226899
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
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now