LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-19-2007, 03:53 PM   #1
mijohnst
Member
 
Registered: Nov 2003
Location: Huntsville, AL
Distribution: RHEL, Solaris, OSX, SuSE
Posts: 419

Rep: Reputation: 31
Bash script with addition...


I'm hoping one of you bash guru's gets this.

I'm grepping through some large files and adding up the occurrences of particular patterns. I've got it where it outputs the number of occurrences from each file, but how to do I get it to add them all together for a total. It's got to be pretty easy I hope. Here is what I have. Any help would be great.

Code:
read -p "What do the file names start with? : " NAME
read -p "What pattern do you want to find? : " PATTERN
LIST=`ls $NAME*.*`

echo ""
echo "############### Here is a list of files ###################"
echo ""
echo $LIST
echo ""

for name in ${LIST} ; do
	echo $name has -
	cat $name | grep -i "$PATTERN" | wc -l
	echo ""
done

exit 0
As an after thought, how do I get the count to show right after "echo $name has -" instead of below it?

Thanks again for the help guys!

Last edited by mijohnst; 04-19-2007 at 03:55 PM.
 
Old 04-19-2007, 04:50 PM   #2
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 97
Hi.

Something like
Code:
read -p "What do the file names start with? : " NAME
read -p "What pattern do you want to find? : " PATTERN
LIST=`ls $NAME*.*`

echo ""
echo "############### Here is a list of files ###################"
echo ""
echo $LIST
echo ""

TOTAL=0

for name in ${LIST} ; do
        THISFILE=`cat $name | grep -i "$PATTERN" | wc -l`
        (( TOTAL += THISFILE ))
	echo "$name has - $THISFILE"
	echo ""
done

echo "Total is: $TOTAL"

exit 0
would probably do the job.

You can get echo to not put a newline at the end, by using 'echo -n' instead of 'echo'.

Obligatory link to ABSG:
http://tldp.org/LDP/abs/html/

Dave

Last edited by ilikejam; 04-19-2007 at 04:51 PM.
 
Old 04-19-2007, 05:02 PM   #3
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
To take your second question first:

There are two echos. One is the program. (On Slackware it's at /usr/bin/echo.) The other is the builtin bash command. They do not behave precisely the same, but in either case the -n option is what you want.

To take your first question:

bash can do arithmetic. Put the arithmetic expression $((here)).

Try this:

Code:
echo -n hi
a=5
echo $a
a=$((a+1))
echo $a
Hope this helps.
 
Old 04-20-2007, 10:14 AM   #4
mijohnst
Member
 
Registered: Nov 2003
Location: Huntsville, AL
Distribution: RHEL, Solaris, OSX, SuSE
Posts: 419

Original Poster
Rep: Reputation: 31
Thank you both! It works perfect now! Works exactly how I wanted it.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
[bash] having trouble debugging this bash script. jons Programming 4 02-08-2007 06:51 AM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
How to make a simple addition in a shell script. philipina Programming 2 03-16-2004 04:38 AM
bash script prob: how can i tell the script that a 'dd' has finished? Frustin Linux - General 2 04-02-2003 05:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration