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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Shell script to search for text in a file and copy file imeadows UNIX for Dummies Questions & Answers 9 11-12-2008 09:12 PM
shell script to edit the content of a file tiger99 Shell Programming and Scripting 3 01-31-2008 04:43 AM
Urgent: selecting unique specific content of a file using shell script jisha Shell Programming and Scripting 2 01-08-2008 08:45 AM
search for the contents in many file and print that file using shell script cdfd123 Shell Programming and Scripting 3 10-07-2007 11:17 PM
Korn Shell Script - Read File & Search On Values run_unx_novice Shell Programming and Scripting 2 06-15-2005 08:20 AM

Closed Thread
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 04-20-2008
psychobeauty psychobeauty is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 17
shell script to search content of file with timestamps in the directory

hello,

i want to make a script to search the file contents in my home directory by a given date and output me the line that has the date...
  #2 (permalink)  
Old 04-20-2008
psychobeauty psychobeauty is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 17
read -p "what date ?" vardate
echo $vardate
awk -v d="$vardate" '$0 ~ d{print}' weblog



i find this one that really works its output me the correct line and everything...
but each time i need to go and change the filename in order to find what i want...
  #3 (permalink)  
Old 04-20-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
I recently responded to a similar question of yours here: Shell script to search for text in a file and copy file


Code:
#!/bin/sh

case $# in 0|1) echo "syntax: $0 date files ..." >&2; exit 2;; esac

date=$1
shift

awk -v d="$date" '$0 ~ d' "$@"

This expects the date as the first parameter, and a list of files as the remaining parameters. Those are passed through to awk in "$@" after the first argument (the date) has been shifted off and passed to the awk script as a variable.

I took out the { print } because that is the default action; this is probably less readable, so if you don't use awk much, it might be safer to leave it in.

Of course, this simple script is exactly equivalent to grep without any options or other bells & whistles.

Last edited by era; 04-20-2008 at 01:45 PM.. Reason: Fix case statement to accept more than one file, duh
  #4 (permalink)  
Old 04-20-2008
psychobeauty psychobeauty is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 17
thanx for replying era!!!

but i am afraid this does not work.
i think that in the script that u write it checking throught files date..
what i want is to check throught files text content...
i have the traffic of my website... in a directory named traffic...
the traffic directory have files with each webpage ip that visit my website!!
so i want the script to be able to output me the ips that visit my website the date that i ask...
  #5 (permalink)  
Old 04-20-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
You might think wrong. Try it.

Like I already wrote, this is equivalent to grep date file

For example, grep 2008-04-20 traffic/127.0.0.1 would search for 2008-04-20 in the file traffic/127.0.0.1.

Maybe your logs use a different date format, but you get the idea.

PS. Simpler still awk script, provided your date format doesn't have slashes in it:


Code:
#!/bin/sh

case $# in 0|1) echo "syntax: $0 date files ..." >&2; exit 2;; esac

date=$1
shift

awk "/$date/" "$@"


Last edited by era; 04-20-2008 at 03:45 PM.. Reason: Clarifying (?) .... grep .... example ....
  #6 (permalink)  
Old 04-20-2008
psychobeauty psychobeauty is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 17
i try it!!!

and it does not work!!
its get me a syntax error..

i used it like this


Code:
#!/bin/sh

read -p "what date" vardate
echo $vardate

case $# in 0|1) echo "syntax: $0 date files ..." >&2; exit 2;; esac

date=$1
shift

awk "/$date/" "$@"


am i wrong somewhere??


my log files text are in this format : 162.12.56.7 Tues Feb 8 21:02:35 GMT 2008
Closed Thread

Bookmarks

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 08:03 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