Can anyone help me to spot my mistake?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Can anyone help me to spot my mistake?
# 1  
Old 08-05-2012
Can anyone help me to spot my mistake?

Hi there can anyone help me to spot my mistake and please explain why it appears
My code :
Code:
#!/usr/bin/gawk -f
BEGIN { bytes =0}
{ temp=$(grep "datafeed\.php" | cut -d" " -f8)
bytes += temp}
END { printf "Number of bytes: %d\n", bytes }

when I am running ./q411 an411
an411:

Code:
152.78.74.111 [13/Feb/2011:06:52:00 +0000] "GET /datafeed.php HTTP/1.1" 200 2826
152.78.74.211 [13/Feb/2011:06:53:00 +0000] "GET /index.html HTTP/1.1" 200 1190
152.78.74.121 [13/Feb/2011:06:55:00 +0000] "GET / HTTP/1.1" 200 3000

It is complaining about cut and closing bracket ")" but I don't understand why.

Any help????
Thanks in advance Smilie

Moderator's Comments:
Mod Comment edit by bakunin: please use CODE-tags not only for code but also for file output. Thank you.

Last edited by bakunin; 08-05-2012 at 07:31 AM..
# 2  
Old 08-05-2012
Code:
temp=$(grep "datafeed\.php" | cut -d" " -f8)

is a shell command; not a gawk command.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 08-05-2012
Quote:
Originally Posted by Don Cragun
Code:
temp=$(grep "datafeed\.php" | cut -d" " -f8)

is a shell command; not a gawk command.
Thanks a lot!! So is there a way to assing output of the command to a variable in gawk ???
# 4  
Old 08-05-2012
I don't know what the construct $(temp...) is doing in your awk program - first, it is a shell variable that is assigned, second, the grep command does not know on what input it should work. If I guess your intentions right to sum up all byte info in the datafeed line, try the following in your awk program:
Code:
/datafeed\.php/ {bytes += $8} END { printf "Number of bytes: %d\n", bytes }

This User Gave Thanks to RudiC For This Post:
# 5  
Old 08-05-2012
If you're trying to get the output RudiC suggested, you don't need awk. You can just do:
Code:
printf "Number of bytes: %d\n" $(grep datafeed.php | cut -d" " -f8) < an411

in the shell. You already had all of the shell code in your awk program.
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 08-05-2012
Quote:
Originally Posted by FUTURE_EINSTEIN
So is there a way to assing output of the command to a variable in gawk ???
In fact there is: use the "system()" function in awk. But to be honest RudiC provided a much better solution if he got your intention right and you should first try to avoid the system()-call.

Every call to an external program is very costly in terms of resources and therefore you should try to solve as much as possible using one command. There is nothing inherently "better" or "worse" in using shell code or awk code, but if you start with awk then stick to it or abandon it altogether. The same goes for shell code.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 7  
Old 08-05-2012
I was mislead by the OP using bytes+=temp into thinking he/she wanted to sum up several lines' byte counts. If there is only one line expected, use Don Cragun's one line shell version.
This User Gave Thanks to RudiC For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Archive script spot a mistake?

#!/bin/bash source=/to_be_archived dest=/archived echo "is this archive for an audio tar press (t) or an audio directory press (d)" read option case $option in d) cd "$source" echo "please specify full path to directory you want to be... (6 Replies)
Discussion started by: robertkwild
6 Replies

2. Shell Programming and Scripting

Strange type mistake?!

Hi, I want to start MY_PROGRAM in a bash script with additional parameters given in the CONFIGURATION_ARRAY. IFS="'" CONFIGURATION_ARRAY=( '-N 0 -m 0' '-N 0 -m 1' ) for configuration in ${CONFIGURATION_ARRAY} do //DEBUG N=${configuration%-*} //-N 0 M=-${configuration##*-} //-m 0... (5 Replies)
Discussion started by: xraystorm
5 Replies

3. AIX

how to make a spot

HI. there My 10 servers are running on 6.1-05-03 aix including NIM server but my user want to update a TL (61-05-03 to 61-06-04) if I Update a AIx version of TL .. is that change the SPOT of NIM server.. OR if I make a spot of AIX7.1 and Is that support AIX 6.1 AIX 5.3 Images I... (1 Reply)
Discussion started by: Jeon Jun Seok
1 Replies

4. UNIX for Dummies Questions & Answers

Probably some stupid mistake...

Hi everyone ! I have a file wich look like this : >Sis01 > Sis02 ... >Sis44 I want to separe each paragraphe in a different file, so I decide to use the "FOR" loop + sed. for f in {01..44} do (5 Replies)
Discussion started by: sluvah
5 Replies

5. Shell Programming and Scripting

Is there any mistake in this code:

cat $1 | sort -n | uniq | $1 in other words, I sort the content of the file and put the ouput in the same file, is there any mistakes in this cshell code ??? (4 Replies)
Discussion started by: Takeeshe
4 Replies

6. AIX

Did a Mistake with HACMP

Hi, I needed space on a FS, and when I've added the space on the filesystem, I did it trough the regular smitty fs inteface and not with smitty cl_lvm. Can someone help me to repair the situat before a faileover happen ? Thanks for your help,:mad: (13 Replies)
Discussion started by: azzed27
13 Replies

7. Shell Programming and Scripting

Spot the difference

I posted earlier with a problem it's here, I have edited the script a little and it tells me once more that the end of line is unexpected and I'm really lost with this one, thanks for any help. The new version: #!/bin/sh case $# in 0) echo "Usage: enshar filename1 filename2 " >&2 ;;... (20 Replies)
Discussion started by: Dim-Wit
20 Replies

8. UNIX for Advanced & Expert Users

Spot

does somebody know about SPOT ?? any link about SPOT commands ? i ve made a mistake during configuration, :mad: then i ve started the system in Maintenance mode the only shell was SPOT. :eek: SPOT doesn t understand anything.... how do i work with SPOT ? thanks in advance Karine... :D (1 Reply)
Discussion started by: karine
1 Replies

9. UNIX for Dummies Questions & Answers

Crontab Mistake!!!

Hi. I hope someone can help me with this problem. Being a novice to Unix, I editted my crontab directly by typing " crontab -e ". Well, I needed to make some changes so, I typed " crontab -r ". Now I have no crontab, and I can't seem to get crontab to write a new file. I' ve tried: vi... (4 Replies)
Discussion started by: cstovall
4 Replies
Login or Register to Ask a Question