The UNIX Forums  



Go Back   The UNIX Forums > Top Forums > Shell Programming and Scripting
Home Forums Register Rules & FAQDonate Members List Search Today's Posts Mark Forums Read

Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

Reply
 
Submit Tools Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 1 Week Ago
Registered User
 
Join Date: May 2008
Posts: 42
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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 : 1 Week Ago at 07:11 AM.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 1 Week Ago
robotronic's Avatar
Can I play with madness?
 
Join Date: Apr 2002
Location: Italy
Posts: 263
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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
Forum Sponsor
  #3 (permalink)  
Old 1 Week Ago
Registered User
 
Join Date: May 2008
Posts: 42
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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 (permalink)  
Old 1 Week Ago
Registered User
 
Join Date: Mar 2006
Location: Mumbai
Posts: 67
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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 : 1 Week Ago at 08:05 AM.
Reply With Quote
  #5 (permalink)  
Old 1 Week Ago
rubin's Avatar
Registered User
 
Join Date: Nov 2007
Posts: 94
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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 (permalink)  
Old 1 Week Ago
Registered User
 
Join Date: May 2008
Posts: 42
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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 (permalink)  
Old 1 Week Ago
rubin's Avatar
Registered User
 
Join Date: Nov 2007
Posts: 94
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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
  #8 (permalink)  
Old 5 Days Ago
robotronic's Avatar
Can I play with madness?
 
Join Date: Apr 2002
Location: Italy
Posts: 263
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Admitted that rubin's way is much more clearer and readable, you can pass variables also interrupting and resuming the awk code by the means of single quotes, like I posted before.

The problem is that before I made a mistake, you also need to add a couple of double quotes to complicate things a little bit more

Code:
awk '$3=="'"$MONTH"'" {print $1, $2, $3, $4, $5, $6}' *.hits
Reply With Quote
  #9 (permalink)  
Old 5 Days Ago
rubin's Avatar
Registered User
 
Join Date: Nov 2007
Posts: 94
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Here goes another unconventional way of passing variables to awk:

Code:
echo "Enter month: "

read MONTH

awk '/'$MONTH'/ { print ... }' file
Caveat emptor:
The above approach does not work if the variable has whitespaces, newlines or certain special characters ( |, /, ...) in it.
So it's always recommended using the conventional way -v var=... .

Last edited by rubin : 5 Days Ago at 04:21 PM. Reason: last sentence
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Proxy ARP Difficulties TheMaskedMan IP Networking 7 11-02-2005 08:14 AM
Simple Network Program Difficulties Mistwolf High Level Programming 2 03-19-2002 04:34 AM


web tracker

All times are GMT -5. The time now is 01:06 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
UNIX Forum Content Copyright ©1993-2008 SilkRoad Asia All Rights Reserved -Ad Management by RedTyger

Search Engine Optimization by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93