How to grep a string in todays file

Hello guys - I am new to Unix.

I am trying to understand how to grep a perticular string in todays file?

I am trying this syntax but not getting what I am looking for:

% grep `date '+%d/%b/%Y'`

For instance there are 2 files generated today with same data. I am trying to find them and then search inside either one of them.

Thank you

Hello,

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 ?

-Nithin.

Hi i am actually looking for a string "lostER" in a file that is generated today.

So would it be like this:

grep -i 'lostER' | grep date '+%d/%b/%Y

I recommend you read the grep man page.
To look for a string in a file :

grep <STRING> <FILE>

No thats not what I am looking for. I already know basic grep commands.

This is about searching for todays file and then looking for a string inside it.

Ok, assuming that today is 5th April, 2010 what would be the name of "today's file" in your file system ?

tyler_durden

It would be something like this:

ERProcessBD04052010*****.txt

The * represent a random changing numeric value.

$
$
$ # 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
$
$

tyler_durden

I can create a file with this command in my system:

touch `date '+%m%d%Y'`.txt

and it give 04052010.txt

and to be able to find a pattern inside that file under a directory:

find . -type f -name `date '+%m%d%Y'`.txt -mtime -1 | xargs grep -i pattern

is this what you are looking for?

Thanks Tyler. The last option you mentioned was the one i was looking for, but it didnt work!

Hi Eagle:

It gives me this error:
find: invalid predicate '- (the file name)

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 ?

tyler_durden

try this one then:

find . -type f -name "*`date '+%m%d%Y'`.txt" -mtime -1 | xargs grep -i pattern

Hi Eagle: I get this 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.

Try this one, it should work like this:

find . -type f -name "ERProcessBD`date '+%m%d%Y'`*" -mtime -1 |  xargs grep "lostER"

or

find . -type f -name "ERProcessBD`date '+%m%d%Y'`*" |  xargs grep "lostER"

Eagle: It now just hangs. Could it because there are two files with same naming convention from todays date?

i am not sure, gimme the list under the directory where your files are and let me check

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.

tyler_durden

If you are trying to find the "lostER" string in a file(s) then use the follloing command : -

$ grep -il loster *.txt

With the above command you can find file names which conatins the string "lostER"

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 i did not get anything back...

Ok as you stated for example you a file say

ERProcessBD060420101270555326

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

following are the results

[root@intnsor-dss07 ~]# ls -lrt ERP*

-rw-r--r-- 1 root root 91567 Apr 6 17:42 ERProcessBD06042010554407000
-rw-r--r-- 1 root root 2037 Apr 6 17:42 ERProcessBD06042010906013000
-rw-r--r-- 1 root root 20536 Apr 6 17:43 ERProcessBD06042010690671000

now i run the command

[root@intnsor-dss07 ~]# grep -Hi "phil" `date +%d%m%Y`

i get

ERProcessBD06042010690671000:05-04 15:41:33 ::User Phil.Edwards added
ERProcessBD06042010690671000:05-04 15:41:53 ::User Phil.Edwards added
ERProcessBD06042010690671000:05-04 19:43:42 ::User Phil.Edwards added
ERProcessBD06042010690671000:05-04 19:46:11 ::User Phil.Edwards added
ERProcessBD06042010690671000:05-04 19:52:34 ::User Phil.Edwards added

I would recommend

grep -Hi "pattern to match" `date +%d%m%Y`