The UNIX and Linux Forums  

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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Proxy ARP Difficulties TheMaskedMan IP Networking 7 11-02-2005 07:14 AM
Simple Network Program Difficulties Mistwolf High Level Programming 2 03-19-2002 03:34 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 05-09-2008
Registered User
 

Join Date: May 2008
Posts: 53
trying to cope with awk difficulties

Quote:
echo "Enter month: "

read MONTH

awk '$3=="$MONTH" {print $1, $2, $3, $4, $5, $6}' *.hits

The data we are searching is populated in this way:

----IP---------DAY----MONTH----DATE--------TIME---------YEAR
12.3234.34-----Fri------Nov-------15-------18:05:14 GMT---2008

I want the user to be able to search for the data according to month and year.
However, I cannot quite figure out how to do this. Above is the code i have and i can't understand what is wrong?

If we try and put in the variable e.g Nov, it wont give us any results.

It works only if instead of "$MONTH" we enter the month itself e.g Nov or Sep.

Last edited by amatuer_lee_3; 05-09-2008 at 05:11 AM.
Reply With Quote
Forum Sponsor
  #2  
Old 05-09-2008
robotronic's Avatar
Can I play with madness?
 

Join Date: Apr 2002
Location: Italy
Posts: 370
You need to add single quotes to pass inside awk the shell variable:

Code:
awk '$3=='"$MONTH"' {print $1, $2, $3, $4, $5, $6}' *.hits
Reply With Quote
  #3  
Old 05-09-2008
Registered User
 

Join Date: May 2008
Posts: 53
Quote:
awk '$3=='"$MONTH"' {print $1, $2, $3, $4, $5, $6}' *.hits
I have tried this and it did not work. I also tried:

Quote:
awk '$3=='$MONTH' {print $1, $2, $3, $4, $5, $6}' *.hits
and this also didnt work.

I'm a bit stumped.
Reply With Quote
  #4  
Old 05-09-2008
Registered User
 

Join Date: Mar 2006
Location: Mumbai
Posts: 67
You need to specify the delimiter used by your flat file in the awk statement..

May not be the case...ignore this

Last edited by Shivdatta; 05-09-2008 at 06:05 AM.
Reply With Quote
  #5  
Old 05-09-2008
rubin's Avatar
Registered User
 

Join Date: Nov 2007
Posts: 216
You need to use AWK's special way of passing variables into it:

Code:
echo "Enter month: "

read MONTH

awk -v mon="$MONTH"  '$3~mon { print $1, $2, $3, $4, $5, $6}' *.hits
Or equally:

Code:
awk '$3~mon { print $1, $2, $3, $4, $5, $6}'  mon="$MONTH" *.hits
Same thing if year is also needed:

Code:
awk -v mon="$MONTH" -v year="$YEAR" '$3~mon && $6~year { print ... } *.hits
Reply With Quote
  #6  
Old 05-09-2008
Registered User
 

Join Date: May 2008
Posts: 53
thanks very much.

can you use AWK to search for unique IP's within this?

so if the same IP logged on more than once it would list the one IP with all the hits?

I already have the IP's in a populated file with the month and date.
Reply With Quote
  #7  
Old 05-09-2008
rubin's Avatar
Registered User
 

Join Date: Nov 2007
Posts: 216
Simply modify the given code:

Code:
echo "Enter month: "

read MONTH

echo "Enter ip: "

read IP


awk -v mon="$MONTH" -v ip="$IP" '$3~mon && $1~ip { print ... }' *.hits
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:18 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0