Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

smart grep for counting numbers

Posted on 2001-06-24
10
Medium Priority
?
245 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 200 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
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 find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Suggested Courses

704 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