I have plain text file "tmp" which include a range of numbers(bytes), say like:
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
I have plain text file "tmp" which include a range of numbers(bytes), say like:
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
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.
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!:o
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
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.
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.
Hi, thanx very much, butterfm and perdo! Your suggestions are extremelly useful, now I can display the total number of byte like something below:
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:eek: .
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
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:
162 /index.html
22 /~bob/
483 /~fred/index.html
Here is the function about this part of my whold code so far:
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.
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.
Hi, butterfm.
Your tips is very helpful for me, now, I have solve the problem, and I would spend some time to understand awk, that is pretty useful. and again, thanx for your help, man.