The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to add numbers email-lalit Shell Programming and Scripting 8 05-12-2008 06:34 PM
need top 3 numbers shary Shell Programming and Scripting 4 03-25-2008 01:58 AM
ksh and hex numbers JamesByars Shell Programming and Scripting 2 01-15-2008 03:36 PM
to count their numbers using awk cdfd123 Shell Programming and Scripting 3 10-15-2007 03:24 AM
Add some numbers! TalkShowHost Shell Programming and Scripting 3 05-15-2002 12:28 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 1.00 average. Display Modes
  #1 (permalink)  
Old 09-09-2003
pnxi pnxi is offline
Registered User
  
 

Join Date: Sep 2003
Location: Austrailia
Posts: 14
Smile How to add numbers?


I have plain text file "tmp" which include a range of numbers(bytes), say like:
Code:
123
234
567
2434
2323
213123
How can I add them and display out.
should I use AWK, then how?
I am a newer in Bourne shell, please give me a hand, thanks a lot
  #2 (permalink)  
Old 09-10-2003
Cameron's Avatar
Cameron Cameron is offline Forum Advisor  
Registered User
  
 

Join Date: Nov 2001
Location: Brisbane, Australia
Posts: 500
Without the need to sound rude or presumptuous, but this could be seen as a homework type question - which is a no-no. Please refer to this thread.

If you post what you've been able to do so far (by means of script/etc); in attempting to solve your own problem - then we can better direct/assist you.
  #3 (permalink)  
Old 09-10-2003
pnxi pnxi is offline
Registered User
  
 

Join Date: Sep 2003
Location: Austrailia
Posts: 14
Red face hi

Hi, Actually, that is a question from my study, but I just ask for a point which I can not handle, I never ask somebody to do my whole job!
What I do so far, I need to sort a range of log file, and caculate the most requested files, total byte transferred and how much percent files has been successfully access. Now I have used AWK
to cut the log file off, and put all the requested number into a tmperory file, now Just don't know any function or comand to use to add all the number together to get the sum, so could anybody to give me a hand.
Thanks
  #4 (permalink)  
Old 09-10-2003
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,119
The bourne shell can't do arithmetic so bourne shell scripts must rely on an external program. expr was written just for this purpose. Try this example:
a=10
b=15
c=`expr $a + $b`
echo $c

There are spaces surrounding that plus sign. expr must see it as a separate argument.

The bourne shell is very old. Modern shells have internal support for arithmetic. You should consider switching shells.

Post the script that you have so far together with a few lines of sample data and we may be able to suggest some improvements.
  #5 (permalink)  
Old 09-10-2003
butterfm butterfm is offline
Registered User
  
 

Join Date: Sep 2003
Posts: 25
Awk is also a natural choice to do this sort of job.

awk 'BEGIN{total=0}
{total += $1}
END{print total}' tmp

Use something like the above but before implementing it into your script get a good understanding of what it does and how it works. That is the only way you will be able to do it on your own next time.

Matt.
  #6 (permalink)  
Old 09-10-2003
pnxi pnxi is offline
Registered User
  
 

Join Date: Sep 2003
Location: Austrailia
Posts: 14
Thumbs up hi, thanx!


Hi, thanx very much, butterfm and perdo! Your suggestions are extremelly useful, now I can display the total number of byte like something below:
Code:
total_byte()
{
  awk '{print $10}' access.log.1 | sort -k 1 | sort -o /tmp/pxi_tmp1
  sum=`awk 'BEGIN{total=0}{total += $1} END{print total}' /tmp/pxi_tmp1` 
  echo "Total byte transmitted: $sum"
}
 if [ $2 = -N ]; then
         echo "Here are $3 most popular files:"
         awk '{print $7}' access.log.1 | sort -k 1 |  uniq -c | sort -ur -o /tmp/pxi_tmp
         head -${3:-10} /tmp/pxi_tmp
	 total_byte
	 total_request
         rm /tmp/pxi_tmp
I have check the fuction of AWK command, it is a big functionality tool, I have to say .
I just have another point which confused me all the time, say if I want to display the files in my tmp file exclude those .gif, .jpg, .jpeg and .png
Code:
  162 /index.html
  129 /title.gif
  22   /~bob/
  483 /~fred/index.html
  13   /~fred/links.gif
  11   /~fred/blog.jpg
(The number in the front is the result of uniq -c, just ignore it)
I would like to display like belows:
Code:
  162 /index.html
  22   /~bob/
  483 /~fred/index.html
Here is the function about this part of my whold code so far:
Code:
elif [ $2 = -I ]; then
         echo "Here are numbers of moset requested files you want:"
         awk '{print $7}' access.log.1 | sort -k 1 |  uniq -c | sort -ur -o /tmp/pxi_tmp
	 ${/tmp/tmp.txt##/*/} >> /tmp/pxi_tmp1	  
	 head -n 10 /tmp/pxi_tmp1
	 total_byte
	 total_request
	 rm /tmp/pxi_tmp1
	 rm /tmp/pxi_tmp
can anybody show me how to achieve that.
  #7 (permalink)  
Old 09-11-2003
butterfm butterfm is offline
Registered User
  
 

Join Date: Sep 2003
Posts: 25
Hi pnxi,

Again using AWK, this will ignore jpg and gif files. You will need to add jpeg and png files.

awk '! ($2 ~ /jpg/ || $2 ~ /gif/) {print $0}' tmp

It's worth taking the time to learn awk if you can. It is extremely powerful, particularly for the sort of thing that you are trying to do.

grep could also be used to do the job.

grep -v "[jg][pi][gf]" tmp

Matt.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:40 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0