LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Recursive search for strings in files with a certain date: find -name or grep -R? (http://www.linuxquestions.org/questions/linux-newbie-8/recursive-search-for-strings-in-files-with-a-certain-date-find-name-or-grep-r-905522/)

wolverene13 09-28-2011 08:57 PM

Recursive search for strings in files with a certain date: find -name or grep -R?
 
Ok, I am trying to find two sets of strings in files created today, within directories that contain a specific string in their name. I have tried different variations of grep and find without the exact results I need. I'll elaborate:

We have numerous devices made by the same vendor, but there are three different models. These devices are in 38 states, with multiple devices in each state. We have used this information to create hostnames for these devices. Here's and example of the format:

rbflftmy81

vendor code = rb
state = fl
city = ftmy
model = SE-800 (hence the "8")
device number = #1 (hence the "1"; subsequent devices in the same city and state would have a 2, or 3, etc.)

These devices get backed up every day, each respective device backing up into its own folder, at it's own time, but the same time each day. The node name and the year, date, and time it backed up are all part of the file name that it backs up to. The directory structure looks like this:

/home/dsl/configs/dslftmyrb--rbflftmy81/dslftmyrb--rbflftmy81.201109271001.config
/home/dsl/configs/dslftmyrb--rbflftmy81/dslftmyrb--rbflftmy81.201109281001.config

I basically need to execute a "grep -A 1" for the strings "port ethernet" and "port atm" on any file created on today's date in any directory with "rb" and "8" in the directory or file name, so I can get all of the strings similar to these:

port atm 6/1
description NF,Ingress,95.MHAZ.784569..UFLG,M4FLFTMYXA01,3B2,622Mb
port ethernet 10/1
description NF,Egress,95.LUXZ.457896..UFLG,A7FTMYFLXA38W,1/1/8,1Gb

I was trying this:

Code:

grep -R -A 1 -e "port ethernet" -e "port atm" /home/configs/dsl/dsl*/dsl*rb*8*.config
...but it doesn't like wildcarding on the directory name.

I also tried this:

Code:

find . -name \*.config | xargs grep rb*8* | grep -A 1 -e "port ethernet" -e "port atm"
But it doesn't work either. Does anyone know how to do this? Moreover, how would I get it to pull files only from today? Sorry if it's wordy and complicated. Thanks in advance!

snooly 09-28-2011 09:05 PM

grep -r is recursive, so you could just point to the parent directory which contains all the files you want to search. It might take a little bit longer, but maybe that doesn't matter.

wolverene13 09-28-2011 09:35 PM

Quote:

Originally Posted by snooly (Post 4484975)
grep -r is recursive, so you could just point to the parent directory which contains all the files you want to search. It might take a little bit longer, but maybe that doesn't matter.

Well, the recursive grep part is working when I just use grep -R -A 1, but that pulls up files for devices with other model numbers too (i.e. rbtxklln01 and rbflwnpk11) and files from the preceding 30 days for the same device. I need to narrow it down to only files in the directory /home/configs/dsl that have the vendor code "rb" and model number "8" in their actual file name, from 9/28/2011. I was thinking that I needed to somehow search for rb......8.20110928*\.config in the name of the file itself, but I have no idea how I would do that while also searching the text within the file for the strings I need also. Any ideas?

snooly 09-28-2011 09:37 PM

You might be able to pipe the results of the first grep into another grep, thus narrowing down the stuff you want.

grep -r stuff-you-want parent-dir | grep -v stuff-you-dont-want

allend 09-29-2011 12:03 AM

You need to enclose the argument to -name in quotes to avoid shell expansion.
Code:

find . -type f -name "*rb??????8??20110928*.config" -print | xargs grep -A 1 -e "port ethernet" -e "port atm"

colucix 09-29-2011 02:17 AM

grep -R should be enough, provided you use the correct path. From your example, it looks like the directories specified in the grep command actually don't exist:
Quote:

/home/dsl/configs/dslftmyrb--rbflftmy81/dslftmyrb--rbflftmy81.201109271001.config
/home/dsl/configs/dslftmyrb--rbflftmy81/dslftmyrb--rbflftmy81.201109281001.config
whereas you specified an additional level in the grep command:
Code:

grep blah blah /home/configs/dsl/dsl*/dsl*rb*8*.config
Anyway, the find command is very useful to restrict the search to recent files, if you base your search on timestamps instead of the date in file names, e.g.
Code:

find /home/configs -type f -name \*rb\*8\*.config -mtime -1 | xargs grep -E -A1 "port ethernet|port atm"
Anyway nothing prevents you to add the date string to the pattern in the -name predicate.

wolverene13 10-01-2011 05:05 PM

Quote:

Originally Posted by colucix (Post 4485236)
grep -R should be enough, provided you use the correct path. From your example, it looks like the directories specified in the grep command actually don't exist:

whereas you specified an additional level in the grep command:
Code:

grep blah blah /home/configs/dsl/dsl*/dsl*rb*8*.config

Yeah, sorry about that, I typed that part into my post manually. In the terminal, I typed it correctly, however. Should have cut and pasted when asking my question.

Quote:

Anyway, the find command is very useful to restrict the search to recent files, if you base your search on timestamps instead of the date in file names, e.g.
Code:

find /home/configs -type f -name \*rb\*8\*.config -mtime -1 | xargs grep -E -A1 "port ethernet|port atm"
Anyway nothing prevents you to add the date string to the pattern in the -name predicate.
But that last thing worked for me, thanks!


All times are GMT -5. The time now is 02:03 PM.