LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-14-2009, 01:34 PM   #1
sebelk
Member
 
Registered: Jan 2007
Posts: 66

Rep: Reputation: 15
Error on awk script


I have log files that looks like below:


Sat Oct 3 11:42:29 2009 : Auth: Login OK: [dpefpe (from client Alpha-2 port 0 via TLS tunnel)
Sat Oct 3 11:47:25 2009 : Auth: Login OK: [bfraser2] (from client Omega port 0 via TLS tunnel)
Sat Oct 3 11:57:54 2009 : Auth: Login OK: [fzoppi] (from client Omega port 0 via TLS tunnel)
Sat Oct 3 11:59:29 2009 : Auth: Login OK: [fzoppi] (from client Epsilon port 0 via TLS tunnel)
Sat Oct 3 12:02:00 2009 : Auth: Login OK: [agfffzoppfi] (from client Beta port 0 via TLS tunnel)
Sat Oct 3 12:03:03 2009 : Auth: Login OK: [gjanfez] (from client Delta port 0 via TLS tunnel)
Sat Oct 3 12:06:59 2009 : Auth: Login OK: [carmefnafp] (from client Gamma port 0 via TLS tunnel)


END OF SAMPLE

I want to get the amount of users that got OK (field 9) classified by client

#! /usr/bin/awk -f
BEGIN {
client[0] = "Alpha"
client[1] = "Beta"
client[2] = "Gamma"
client [3] = "Delta"
client [4] = "Epsilon"
client [5] = "Omega"

for(key in client){
{if($9 ~ /OK/ && $13 ~ /client[key]/)

{print $10} | sort -fu | wc -l
}
}
}


END of Script

But that exits with the followinf error:

$ ./qqq /var/log/radius/radiusd-inner-tunnel-20091003.log
awk: ./qqq:13: {print $10} | sort -fu | wc -l
awk: ./qqq:13: ^ syntax error
awk: ./qqq:13: {print $10} | sort -fu | wc -l
awk: ./qqq:13: ^ syntax error


Please could you help me to fix it?

Thanks in advance!
 
Old 10-14-2009, 01:50 PM   #2
wfh
Member
 
Registered: Sep 2009
Location: Northern California
Distribution: Ubuntu Debian CentOS RHEL Suse
Posts: 164

Rep: Reputation: 44
Using your original log entries (myfile) I get this:

fgrep OK myfile|awk '{print $13}'|sort -u|wc

5 5 33
 
Old 10-14-2009, 06:44 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
of course you will get errors. You are mixing shell commands inside awk.
 
Old 10-15-2009, 06:52 AM   #4
sebelk
Member
 
Registered: Jan 2007
Posts: 66

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by ghostdog74 View Post
of course you will get errors. You are mixing shell commands inside awk.
OK, ok, but even if I use:


Code:
#! /usr/bin/awk -f
BEGIN {
        client[0] = "Alpha"
        client[1] = "Beta"
        client[2] = "Gamma"
        client [3] = "Delta"
        client [4] = "Epsilon"
        client [5] = "Omega"

        for(key in client){
                {if($9 ~ /OK/ && $13 ~ /client[key]/)

                        {print $10}
                }
        }
}
I get nothing! I expect that print 10th field (username)...
 
Old 10-15-2009, 02:31 PM   #5
PMorph
Member
 
Registered: Sep 2003
Distribution: Debian
Posts: 213

Rep: Reputation: 31
Dunno why that doesn't work (can't use variables as regexp?), but it seems to work if you replace:
if($9 ~ /OK/ && $13 ~ /client[key]/)

with:
if($9 ~ /OK/ && match($13, client[z]))

Edit: Scratch that.. your original form works if you just remove the slashes around the client[key]

Last edited by PMorph; 10-15-2009 at 03:14 PM. Reason: Answered the right question this time
 
Old 10-15-2009, 07:31 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Hint: field variables $1...$n are only available outside the BEGIN block.
 
Old 10-16-2009, 07:44 AM   #7
sebelk
Member
 
Registered: Jan 2007
Posts: 66

Original Poster
Rep: Reputation: 15
Thanks everyone, at the end I've found a way to do that almost all in a mix of bash, awk and GNU tools:

Code:
for i in "(.*Alpha.*)" "(.*Beta.*)" "(.*Gamma.*)" "(*.Deltha.*)" 
"(*.Gamma.*)" "(*.Epsilon.*)"; do grep $i "/var/log/radius/radiusd-inner-
tunnel-20090925.log" | echo "$i": $(awk '$9 ~ OK {print  $10 }'   | sort -
fu| wc -l); done
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script is enterpreting $1, $2 values in awk script ... praveen_218 Programming 4 09-14-2009 03:38 PM
awk shell script error ApacheRoseXbones Linux - Newbie 5 07-18-2008 12:06 PM
bash script read error and awk ouptut error whited Programming 4 10-16-2007 07:05 PM
About awk script sachin_keluskar Linux - Software 2 06-24-2005 03:19 AM
Passing variables from AWK script to my shell script BigLarry Programming 1 06-12-2004 04:32 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:27 AM.

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