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
# 8  
Old 04-20-2008
Quote:
Originally Posted by era
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).


itry this one also era when i execute it it tell me

Code:
syntax: files...

i think the error might be on the
Code:
case $# in 0) echo syntax: $0 files ...; exit 2;; esac

# 9  
Old 04-21-2008
You invoke it on the files you want to search. If you called the script datefind, you'd run it like

Code:
./datefind traffic/127.0.0.1

and it will prompt you for the date, and search in the file traffic/127.0.0.1 for the given date.

Once again, it doesn't really offer any advantage over regular grep, unless you specifically want the interactive prompting for a date. (Saves you from quoting the date, too.)
# 10  
Old 04-21-2008
Code:
read var
for i in *
do
grep $var $i
if [ $? -le 0 ] 
then
echo $1:`grep $var $i`
fi
done

# 11  
Old 04-21-2008
summer_cherry: you need to double-quote $var, as it might contain spaces.

Also, running grep twice seems kind of excessive. Already the first invocation will print the match (unless you redirect or suppress it), albeit without the "$1": prefix. If you meant to print the file name, that's not entirely correct; you are looping over files with the file name in $i, so that is likely what you intended to print, but you can do that with grep "$var" $i /dev/null or -- in some versions -- grep -H

Anyway, the loop isn't really necessary, because grep can handle multiple input files.

Code:
#!/bin/sh

echo -n 'Type in the date: '
read date
grep "$date" *

This will search all files in the current directory. Perhaps that's what the OP wants, although the problem description mentions a subdirectory with log files, from which (as I understand it) only one is to be searched at a time.

Last edited by era; 04-21-2008 at 06:40 AM.. Reason: Pass wildcard to grep
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