The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Passing input to perl command john_noble Shell Programming and Scripting 1 08-13-2009 01:32 PM
passing an input parameter like date thru jcl using BPXBATCH utility Sujatha Gowda SUN Solaris 0 06-03-2008 09:23 AM
Passing Value from Shell to Perl hcbhatt Shell Programming and Scripting 5 11-05-2007 02:31 PM
Passing Value from awk to shell raman1605 Shell Programming and Scripting 3 08-06-2007 06:25 AM
passing value to shell variable trynew Shell Programming and Scripting 2 06-24-2002 03:13 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-22-2009
wawa44oz wawa44oz is offline
Registered User
  
 

Join Date: Sep 2009
Posts: 2
Passing Shell Input to AWK

I am trying to search a log for a particluar pattern listing the total # of occurences in the end.

I thought using a shell script for input then calling awk to search for the paramters specified. I want the script to be usable acorss envs.

Code:

Code:
#! /usr/bin/bash
# get the variables
echo -n "1.  LogFILE ?"
read LOG
echo -n "2. SEARCH FOR ? "
read word
echo -n "3. Timestamp start = "
read time_st
echo -n "4. Timestamp end = "
read time_en
# search the file
awk  -v"1=${word}" "2=${time_st}" "3=${time_en}" "4=${log}" | wc


Source: Items in read is what I would like to search on.

Code:
10.7.90.85 - - [22/Sep/2009:10:02:24 -0400] "GET /portal/framework/skins/cafe/css/body.css HTTP/1.1" 200 1304
10.7.90.85 - - [22/Sep/2009:14:06:32 -0400] "GET /portal/cafekeepalive.jsp?NONE HTTP/1.1" 200 21

Output:
"CSS.sh" 12 lines, 287 characters

Code:
bash-3.00$  ./CSS.sh
1.  LogFILE ?log.log
2. SEARCH FOR ? cafe
3. Timestamp start = 10:0*
4. Timestamp end = 14:0*
awk: syntax error near line 1
awk: bailing out near line 1


Thanks in advance for any assitance

Last edited by vgersh99; 09-22-2009 at 04:40 PM.. Reason: code tags, PLEASE!
  #2 (permalink)  
Old 09-22-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
  #3 (permalink)  
Old 09-22-2009
varontron varontron is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 110
not sure awk is the right tool here.

you can 'grep -n' to get the line number of each $time_st and $time_en
you can use a combination of 'head' and 'tail' to output the subset of lines between $time_st and $time_en you want to search
you can use 'grep -c' to get the number of occurances of $word in that subset

hth,
dv
  #4 (permalink)  
Old 09-23-2009
wawa44oz wawa44oz is offline
Registered User
  
 

Join Date: Sep 2009
Posts: 2
DV -
Thanks for the response...

Am heading down the right direction with this?


Code:
cat access.log.1253664000 | egrep "bank.cssbrowser.com" | egrep "10:0*" | egrep "11:*" head -1

TIA.

Last edited by wawa44oz; 09-23-2009 at 02:28 PM..
  #5 (permalink)  
Old 09-23-2009
Corona688 Corona688 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 1,969
Quote:
Originally Posted by wawa44oz View Post
DV -
Thanks for the response...

Am heading down the right direction with this?


Code:
cat access.log.1253664000 | egrep "bank.cssbrowser.com" | egrep "10:0*" | egrep "11:*" head -1

TIA.
You don't need to use cat here.

Your greps need some escaping. Right now the . means 'match any character', not 'match .'

And your greps won't return anything because first you reject everything except your start time, and after that you reject everything except your end time. You need to match start and end and everything inbetween. Since grep doesn't understand what date and time means -- or what digits mean, for that matter -- I don't think grep can do what you want all by itself. There's several things with : in it that could match anyway, so that probably won't narrow it down to what you want. I'll work on this a bit...

---------- Post updated at 12:38 PM ---------- Previous update was at 12:25 PM ----------

Here's how you'd match exactly the hours you want.
Code:
#!/bin/sh

# Build a list of the hours we want, to fill into egrep
START=11
END=14
STR=$START
for ((N=START+1; N<=END; N++))
do
        STR="$STR|$N"
done

# This will match 14:06 but not 10:02 since it starts at 11
echo -e "[22/Sep/2009:10:02:24 -0400]\n[22/Sep/2009:14:06:32 -0400]" |
        egrep "/[0-9]+:($STR):"

You could tack another grep to match a specific hostname or what have you. It's not too sophisticated. You can't take it too much farther because grep can't understand what the dates actually mean.

This is about as complex as I'd bother making it in a shell script, since shell in general has a hard time processing date information. If you want something smart enough to just specify a beginning time and date and end time and date, I'd just use perl and process the dates wholesale to compare them.
  #6 (permalink)  
Old 09-23-2009
varontron varontron is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 110
ok, so in my orig response I was thinking this:


Code:
start_time=10
end_time=14
word=cafe
# the number of the first line in the file with the start time and word
STARTLINE=grep -n ":$start_time:[0-5][0-9]:.*$word"|head -1|cut -f1 -d':'

# the number of the last line in the file with the end time and word
ENDLINE=grep -n ":$end_time:[0-5][0-9]:.*$word"|tail -1|cut -f1 -d':'

# the subset of lines between STARTLINE and ENDLINE
head -$ENDLINE file | tail -n +$STARTLINE > subsetfile

# the number of lines in subsetfile containing $word
grep -c $word subsetfile

regexes here will work, but could be improved.
Reply

Bookmarks

Tags
awk, script, shell

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:07 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0