How to grep a string in todays file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to grep a string in todays file
# 15  
Old 04-05-2010
Eagle: It now just hangs. Could it because there are two files with same naming convention from todays date?
# 16  
Old 04-05-2010
Quote:
Originally Posted by DallasT
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
# 17  
Old 04-05-2010
Quote:
Originally Posted by DallasT
...
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 -

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

Code:
grep "lostER" <hard_coded_todays_file_name>

Again, how long does the left part of the pipeline take ? That is -

Code:
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
# 18  
Old 04-06-2010
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"
kandi.reddy
# 19  
Old 04-06-2010
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...

Last edited by DallasT; 04-06-2010 at 09:02 AM..
# 20  
Old 04-06-2010
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`*
# 21  
Old 04-06-2010
Quote:
Originally Posted by DallasT
...
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...
Use egrep (extended grep) or grep -E

Code:
$
$ ls -1 ERProcess*.txt
ERProcessBD0404201099999.txt
ERProcessBD0405201099999.txt
ERProcessBD0406201099999.txt
$
$
$ cat ERProcessBD0406201099999.txt
04/06/2010 line_1 => blah
04/06/2010 line_2 => lostER
04/06/2010 line_3 => blah
04/06/2010 line_4 => SystemOff
$
$
$ egrep "lostER|SystemOff" ERProcessBD`date '+%m%d%Y'`*.txt
04/06/2010 line_2 => lostER
04/06/2010 line_4 => SystemOff
$
$ grep -E "lostER|SystemOff" ERProcessBD`date '+%m%d%Y'`*.txt
04/06/2010 line_2 => lostER
04/06/2010 line_4 => SystemOff
$
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace string of a file with a string of another file for matches using grep,sed,awk

I have a file comp.pkglist which mention package version and release . In 'version change' and 'release change' line there are two versions 'old' and 'new' Version Change: --> Release Change: --> cat comp.pkglist Package list: nss-util-devel-3.28.4-1.el6_9.x86_64 Version Change: 3.28.4 -->... (1 Reply)
Discussion started by: Paras Pandey
1 Replies

2. UNIX for Beginners Questions & Answers

How to list todays file in perticular folder?

How to list todays file in perticular folder Moved thread to appropriate forum (9 Replies)
Discussion started by: pspriyanka
9 Replies

3. Shell Programming and Scripting

Grep string in files and list file names that contain the string

Hi, I have a list of zipped files. I want to grep for a string in all files and get a list of file names that contain the string. But without unzipping them before that, more like using something like gzcat. My OS is: SunOS test 5.10 Generic_142900-13 sun4u sparc SUNW,SPARC-Enterprise (8 Replies)
Discussion started by: apenkov
8 Replies

4. Shell Programming and Scripting

grep exact string from files and write to filename when string present in file

I am attempting to grep an exact string from a series of files within a directory and append that output to the filename when it is present in the file. I've been after this all day with no luck. Thanks for your help in advance :wall:. (4 Replies)
Discussion started by: JC_1
4 Replies

5. Shell Programming and Scripting

Find and copy files based on todays date and search for a particular string

Hi All, I am new to shell srcipting. Problem : I need to write a script which copy the log files from /prod/logs directory based on todays date like (Jul 17) and place it to /home/hzjnr0 directory and then search the copied logfiles for the string "@ending successfully on Thu Jul 17". If... (2 Replies)
Discussion started by: mail.chiranjit
2 Replies

6. Shell Programming and Scripting

Grep a string from input file and delete next three lines including the line contains string in xml

Hi, 1_strings file contains $ cat 1_strings /home/$USER/Src /home/Valid /home/Review$ cat myxml <projected value="some string" path="/home/$USER/Src"> <input 1/> <estimate value/> <somestring/> </projected> <few more lines > <projected value="some string" path="/home/$USER/check">... (4 Replies)
Discussion started by: greet_sed
4 Replies

7. Shell Programming and Scripting

how to get todays file name

Hi ! there can you please tell me how to get full files having today date like if i have files like this(below) , i want to grep only sra + today's date + what ever thing is there after date . grep `sra*$date*.csv` >>> i tried this one but its not working . sra28-08-2011xyz.csv... (1 Reply)
Discussion started by: sravan008
1 Replies

8. Shell Programming and Scripting

How to list todays file

Hi Friends, How to list todays file from a directory listing of files for amny dates. I tried with the following options but not working : find . -name "esi01v*" -mtime 1 -ls find . -name "esi01v*" -ctime 1 -ls find . -name "esi01v*" -mtime 1 Please advise (19 Replies)
Discussion started by: unx100
19 Replies

9. Shell Programming and Scripting

using todays date to create a report using grep

What i'm trying to do is to use grep to search through a few files for a selected daemon and only report on today's date. I think I got it sorted apart from in the txt file the date has 2 gaps between the month and the day, and the way I have the date format only puts in one gap any help to get... (3 Replies)
Discussion started by: MBN
3 Replies

10. Shell Programming and Scripting

want to delete timestamp from todays file

i want to delete the time stamp from the file which have a date of yester day 640878 Nov 6 09:08 fbres.01.20031106:09:08:30 here is what my ls -lt command shows in current directory it want it to be 640878 Nov 6 09:08 fbres.01.20031106 thanks (5 Replies)
Discussion started by: maverick
5 Replies
Login or Register to Ask a Question