Solved

smart grep for counting numbers

Posted on 2001-06-24
10
241 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
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 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…

749 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