shell script to search content of file with timestamps in the directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell script to search content of file with timestamps in the directory
# 1  
Old 04-20-2008
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  
Old 04-20-2008
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  
Old 04-20-2008
I recently responded to a similar question of yours here: https://www.unix.com/unix-dummies-que...#post302187223

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  
Old 04-20-2008
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  
Old 04-20-2008
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  
Old 04-20-2008
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
# 7  
Old 04-20-2008
What's the syntax error? And why did you add the "read vardate" stuff? You're supposed to pass in the date on the command line. Of course, you can change that, but then you need to modify the rest of the script accordingly.

When passing a value with spaces in it, you need to use quotes:

Code:
grep "Tues Feb 8" traffic/127.0.0.1

Here's another try at a script:

Code:
#!/bin/sh

case $# in 0) echo syntax: $0 files ...; exit 2;; esac

echo enter date:
read date

grep "$date" "$@"

The script will be more useful for use from other scripts if you let it accept the date on the command line rather than read it interactively, but that may not be an issue here. Anyway, you need to pass it the file(s) to search as arguments anyway, so why not make the date a command-line argument, too (or else ask which files to grep, interactively).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search lines between two timestamps in a file

Hi, I want to pull lines between two timestamps from a file. When I just insert values directly it is working fine. sed -n '/2014-02-14 05:30/,/2014-02-14 05:31/p' Logfile When I try to insert variable it is not working. sed -n '/TZ=GMT+1 date +%Y-%m-%d" "%H:%M:%S/,/TZ=GMT date... (2 Replies)
Discussion started by: Neethu
2 Replies

2. Shell Programming and Scripting

Shell script to monitor new file in a directory and mail the file content

Hi I am looking for a help in designing a bash script on linux which can do below:- 1) Look in a specific directory for any new files 2) Mail the content of the new file Appreciate any help Regards Neha (5 Replies)
Discussion started by: neha0785
5 Replies

3. Shell Programming and Scripting

Script to process file from a directory and grep the required content and print

Hi All, below script reads the perticular files from the directory. Am trying to fetch status and print them in the required format. It needs to read line and search for string "Passed/Failed" and print them under correct sub header. script : BASE_DIR=/tmp/test/REPORT/CollectReport #... (16 Replies)
Discussion started by: Optimus81
16 Replies

4. Shell Programming and Scripting

To search lines between two timestamps in a file

I have a log file where every line starts with a time stamp. I have to extract lines from the file within a given time stamp. For example: IF the file is like this: 2012-08-19 10:34:03,446|WebContainer : 56|OrderHeaderDaoImpl|findByKeys|26| 2012-08-20 11:34:03,463|WebContainer :... (8 Replies)
Discussion started by: abhinalluri
8 Replies

5. Shell Programming and Scripting

Shell script to search a pattern in a directory and output number of find counts

I need a Shell script which take two inputs which are 1) main directory where it has to search and 2) pattern to search within main directory all files (.c and .h files) It has to print number of pattern found in main directory & each sub directory. main dir --> Total pattern found = 5 |... (3 Replies)
Discussion started by: vivignesh
3 Replies

6. Shell Programming and Scripting

Need to build Shell Script to search content of a text file into a folder consist several files

Have to read one file say sourcefile containing several words and having another folder containing several files. Now read the first word of Sourcefile & search it into the folder consisting sevral files, and create another file with result. We hhave to pick the filename of the file in which... (3 Replies)
Discussion started by: mukesh.baranwal
3 Replies

7. Shell Programming and Scripting

Shell script to remove some content in a file

How can I remove all data that contain domain e.g zzgh@something.com, sdd@something.com.my and gg@something.my in one file? so that i only have data without the domain in the file. Here is the file structure "test.out" more test.out 1 zzztop@b.com 1 zzzulll 1 zzzullll@s.com.my ... (4 Replies)
Discussion started by: Mr_47
4 Replies

8. Shell Programming and Scripting

Help with Compare and search content in directory

Hello, How to search in directory by comparing some string with the content of directory. Ex: I want to compare abhi string with the content of backup directory. i.e want to check that is there any file in backup directory having name ... (3 Replies)
Discussion started by: AbhijitIT
3 Replies

9. Shell Programming and Scripting

shell script to search a string and delete the content

Hi, I've a shell script e.g. #!/bin/bash echo "Enter the next hop id" read nhid echo "enter the IP address" read IP echo "enter the interface name" read name echo "enter the enable/disable state" read state exit 0 now from this script i want to search strings in another (.cam) ... (6 Replies)
Discussion started by: vic_mnnit
6 Replies

10. Shell Programming and Scripting

shell script to edit the content of a file

Hi I need some help using shell script to edit a file. My original file has the following format: /txt/email/myemail.txt /txt/email/myemail2.txt /pdf/email/myemail.pdf /pdf/email/myemail2.pdf /doc/email/myemail.doc /doc/email/myemail2.doc I need to read each line. If the path is... (3 Replies)
Discussion started by: tiger99
3 Replies
Login or Register to Ask a Question