LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 09-28-2011, 08:57 PM   #1
wolverene13
Member
 
Registered: May 2010
Location: Matiland, FL
Distribution: Debian Squeeze
Posts: 49

Rep: Reputation: 0
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!
 
Old 09-28-2011, 09:05 PM   #2
snooly
Member
 
Registered: Sep 2011
Posts: 124

Rep: Reputation: Disabled
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.
 
Old 09-28-2011, 09:35 PM   #3
wolverene13
Member
 
Registered: May 2010
Location: Matiland, FL
Distribution: Debian Squeeze
Posts: 49

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by snooly View Post
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?
 
Old 09-28-2011, 09:37 PM   #4
snooly
Member
 
Registered: Sep 2011
Posts: 124

Rep: Reputation: Disabled
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
 
Old 09-29-2011, 12:03 AM   #5
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 3,378

Rep: Reputation: 824Reputation: 824Reputation: 824Reputation: 824Reputation: 824Reputation: 824Reputation: 824
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"
 
Old 09-29-2011, 02:17 AM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,453

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
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.

Last edited by colucix; 09-29-2011 at 02:18 AM.
 
Old 10-01-2011, 05:05 PM   #7
wolverene13
Member
 
Registered: May 2010
Location: Matiland, FL
Distribution: Debian Squeeze
Posts: 49

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
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!
 
  


Reply

Tags
find, grep, recursive, xargs


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
using find or grep to find a group of text strings. tkmsr Linux - Newbie 3 03-04-2011 07:02 AM
[SOLVED] Grep Recursive search directory depth option? timdvtemp Linux - Software 1 02-28-2011 03:49 PM
How to search date period in access log using grep bilalcochin Linux - Newbie 2 02-19-2010 08:24 AM
Grep Recursive Search Results Send To E-mail or Log? Knucklehead00 Linux - Newbie 2 06-05-2009 10:19 AM
Grep search & replace strings with backslashes? ziphem Linux - Newbie 4 10-21-2007 04:36 PM


All times are GMT -5. The time now is 06:20 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration