shell scripting help/advice


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers shell scripting help/advice
# 1  
Old 10-20-2009
Computer shell scripting help/advice

hello i am trying to generate a list of the most bandwidth consuming ip's from an Apache style log file. the script is run with the path to the log file as the only peramater.

here is my code so far:

Code:
echo "The ten most bandwidth consuming IP's were:"
$outpt=''
for ip in $(awk '{print $1}' "$1" | sort | uniq); do

outpt="$outpt $(grep $ip $1 | awk '{sum+=$10} END {print sum/1024,"KB",$1}') "

done

echo $outpt

im very new to shell scripting and am having problems storing the results of my for loop in the variable $outpt so that i could perform a sort -rn | head -10 on the data.

also at the begining of the loop
"=: not found"

is outputed to the screen. any help would be much appriciated.
# 2  
Old 10-20-2009
Hi.

I don't know what shell you are using, but most likely, this is wrong:

Code:

$outpt=''

Remove the $ and try it again.

It's possibly better to use "unset outpt" than outpt=''
# 3  
Old 10-20-2009
Ok thanks for that i guess you don't need to declare a variable so i just removed that line and the error is removed. i guess what im still lacking is a way to insert a newline after each entry. I don't know if i can do this within my for loop or if i have to parse it with sed or something so i can use it as a list for sort

ps. i am using !#/bin/sh
# 4  
Old 10-20-2009
Code:
nawk '{sum[$1]+=$10}END {for(i in sum) print sum[i]/1024, "KB", i}' myApacheLogFile | sort -nr -k1,1 | head -10


Last edited by vgersh99; 10-21-2009 at 09:06 AM.. Reason: ooops - fixed, thanks qcent
# 5  
Old 10-20-2009
thanks vgersh99 but i get an error when i run that line:
nawk: can't read value of sum; it's an array name.
input record number 1386, file /var/log/httpd-access.log
source line number 1


edit
---------------
ok i fix the line to read
Code:
nawk '{sum[$1]+=$10}END {for(i in sum) print sum[$i]/1024, "KB", i}' "$1" | sort -nr -k1,1 | head -10

and it give almost a perfect output except that sum[$i] is always null and it is sorting by ip address not bandwidth usage

re-edit
-----------------
i got it just by removing the "$" from i thank you for this vgersh it also appears to be much faster then my original method

Last edited by qcent; 10-20-2009 at 08:25 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help! Basic shell script advice

##### (2 Replies)
Discussion started by: AidoPotato
2 Replies

2. Shell Programming and Scripting

Advice required shell script

Hi all, this is my first post, so please be gentle... I have a situation wherby I need a script that traverses known paths. Check for the modified date (n days) and then deletes all subdirs. I have come up with this hotch potch, but as far as I can tell it seems to work. What I am wondering... (4 Replies)
Discussion started by: primus7
4 Replies

3. Linux

Scripting advice needed

Evening all, Im trying to get a script that will: Select the most 3 recent files in a specific directory Run a command on them (like chmod) Ask of you would like to continue Copy the files to another directory If a linux guru could help me out, it would be very much appreciated. Thanks... (2 Replies)
Discussion started by: Wiggins
2 Replies

4. UNIX for Dummies Questions & Answers

Few shell programs advice

Hy i have some tasks to do in school but i'm having problems with it,so could you help me out? :) first there is a task where i have to find a running program on the system and kill it, then repeat that every 5 minutes. The name of the process is given with an argument. I have done this so far,... (1 Reply)
Discussion started by: petel1
1 Replies

5. Shell Programming and Scripting

Call Shell scripting from Perl Scripting.

Hi How to call a shell scripting through a Perl scripting? Actually I need some value from Shell scripting and passes in the Perl scripting. So how can i do this? (2 Replies)
Discussion started by: anupdas
2 Replies

6. UNIX for Advanced & Expert Users

Career advice for experienced shell script coder, need help.

I have four years of shell scripting experience in AIX and HP-UX and have worked in perl scripts as well, the good part is i love scripting and so far i have been getting job offers as well. The bad part is , shell scripting is all i know , so the kind of jobs i am getting is mostly production... (2 Replies)
Discussion started by: harishrao
2 Replies

7. Shell Programming and Scripting

advice on shell script

Hello, I have this script running on cron every 20 minutes. By 12pm daily, our system is expecting all input files to be uploaded by the script. After this cutoff time, the script would still be running though, but i need some kind of alerts/logs to know which input files weren't received for... (1 Reply)
Discussion started by: gholdbhurg
1 Replies

8. UNIX for Advanced & Expert Users

scripting advice

what is the good way to improve your skill in shell scripting?? (4 Replies)
Discussion started by: memphiz16
4 Replies

9. Shell Programming and Scripting

difference between AIX shell scripting and Unix shell scripting.

please give the difference between AIX shell scripting and Unix shell scripting. (2 Replies)
Discussion started by: haroonec
2 Replies

10. Shell Programming and Scripting

c-shell script advice please.

Hi, I have the following script running in my cron. -------------------------------------------------------------------- #!/bin/csh bnstat -p GPD_VSLinux | grep pg | grep varcon | awk '{print $1, $2, $3, $4, $5, $6, $7, $8, $9, $10}' > /tmp/LX_xbatch.log bnstat -p GPD_VSLinux_test | grep pg... (2 Replies)
Discussion started by: killerserv
2 Replies
Login or Register to Ask a Question