You should give 2 arguments to grep command. 1 is search string (`date '+%d/%b/%Y'`) in your case & file to search for. So you want to find (date '+%d/%b/%Y') string in file ?
$
$
$ # list all files that start with "ERProcess"
$ ls -1 ERProcess*
ERProcessBD0404201099999.txt
ERProcessBD0405201099999.txt
$
$ # show the contents of yesterday's file
$ cat ERProcessBD0404201099999.txt
04/04/2010 line_1 => blah
04/04/2010 line_2 => lostER
04/04/2010 line_3 => blah
$
$ # show the contents of today's file
$ cat ERProcessBD0405201099999.txt
04/05/2010 line_1 => blah
04/05/2010 line_2 => lostER
04/05/2010 line_3 => blah
$
$ # list just today's file
$ ls -1 ERProcessBD`date '+%m%d%Y'`*.txt
ERProcessBD0405201099999.txt
$
$ # list just today's file and then grep for the word "lostER" in it
$ ls -1 ERProcessBD`date '+%m%d%Y'`*.txt | xargs grep "lostER"
04/05/2010 line_2 => lostER
$
$
What's the error message that you see on your terminal ?
Can you copy/paste the part of your terminal screen where you run the command and encounter the error ?
ERProcessBD04052010*.txt: No such file or directory
Hi Tyler, once i execute the command, it just sits and nothing happens.
I would also want to inform that there are 2 files from todays date in the same folder. They both are duplicates of each other. I wouldnt care which one does the command read, it can be either of the two. They both however have the same naming convention but are named differently.
I've cooked up a testcase that simulates the "2 file" scenario. Of course, the files cannot have the same name, and I am assuming that the 5 digit random number between the date format and the ".txt" extension is different between their names.
In that case, the ls -1 and the subsequent pipeline to grep will just print the lines from both files that have "lostER" in them. See below -
$
$
$ # list all files that start with "ERProcess"
$ ls -1 ERProcess*
ERProcessBD0404201099999.txt
ERProcessBD0405201088888.txt
ERProcessBD0405201099999.txt
$
$ # show the contents of yesterday's file
$ cat ERProcessBD0404201099999.txt
04/04/2010 line_1 => blah
04/04/2010 line_2 => lostER
04/04/2010 line_3 => blah
$
$ # show the contents of today's file file_1
$ cat ERProcessBD0405201088888.txt
04/05/2010 file_1 line_1 => blah
04/05/2010 file_1 line_2 => lostER
04/05/2010 file_1 line_3 => blah
$
$ # show the contents of today's file file_2
$ cat ERProcessBD0405201099999.txt
04/05/2010 file_2 line_1 => blah
04/05/2010 file_2 line_2 => blah
04/05/2010 file_2 line_3 => lostER
$
$ # list just today's file(s)
$ ls -1 ERProcessBD`date '+%m%d%Y'`*.txt
ERProcessBD0405201088888.txt
ERProcessBD0405201099999.txt
$
$ # list just today's file(s) and then grep for the word "lostER" in it
$ ls -1 ERProcessBD`date '+%m%d%Y'`*.txt | xargs grep "lostER"
ERProcessBD0405201088888.txt:04/05/2010 file_1 line_2 => lostER
ERProcessBD0405201099999.txt:04/05/2010 file_2 line_3 => lostER
$
$
But you say that it just sits there and nothing happens.
Are you saying that the control does not return to the shell's dollar ($) prompt ?
The only thing I can think of then is that your files are huge and the grep is still working and taking its own time.
How big are the files ?
How long does it take to grep a hard-coded file name ? That is -
grep "lostER" <hard_coded_todays_file_name>
Again, how long does the left part of the pipeline take ? That is -
ls -1 ERProcessBD`date '+%m%d%Y'`*.txt
By testing individual commands, you should be able to figure out the exact process that's hogging up all the time.
Hi tyler_durden, you were right the files were almost 100 MB in size and constantly increasing. I just ran the command now when the file is small and it works like a charm! Thank you so much!
I have a question: How do i define more than one word in the grep command? I did this:
ls -1 ERProcessBD`date '+%m%d%Y'`*.txt | xargs grep "lostER", "SystemOff"
and u may have many of them with 06042010 common in them.
so if you are looking for a pattern you want in all the files generated today( which according to you would have a pattern of date +%d%m%Y in there file name.
Then i would suggest you run the command
grep Hi "desired pattern" `date +%d%m+Y`
I am running RHEL 5 and i actually simulated what you said