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
Medium Priority
245 Views
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
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
• 4
• 3
• 3

LVL 84

Expert Comment

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

LVL 38

Expert Comment

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

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

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

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

ID: 6223576
What do you get from
perl -v
0

LVL 6

Author Comment

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

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

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

LVL 6

Author Comment

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

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
Course of the Month6 days, 7 hours left to enroll