Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

smart grep for counting numbers

Posted on 2001-06-24
10
Medium Priority
?
246 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 85

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
LVL 85

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 200 total points
ID: 6224426
Hi Talmash,

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

Regards
   yuzh
0
 
LVL 85

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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…
Suggested Courses

963 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