LinuxQuestions.org
Visit Jeremy's Blog.
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 01-19-2010, 04:00 AM   #1
guessity
Member
 
Registered: Dec 2009
Posts: 41

Rep: Reputation: 15
How to use find with two lines together?


I am trying to search for
Code:
<cats>
Sports
</cats>
how do i use the find command to search for <cats>Sports

The problem here is that the word Sports is in the next line of <cats> so I cant use -

Code:
find /test/upload/ -type f -name "*.xml" -exec grep -l "<cats>Sports" {} \; -print
How do I put the next line string? plz help..
 
Old 01-19-2010, 05:21 AM   #2
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 356Reputation: 356Reputation: 356Reputation: 356
Try this:
Code:
find /test/upload/ -type f -name "*.xml" -exec grep -l -A 1 -B 1 "Sports" {} \; -print
-A 1 print one line After the line containing Sports
-B 1 print one line Before the line containing Sports
 
Old 01-19-2010, 05:49 AM   #3
guessity
Member
 
Registered: Dec 2009
Posts: 41

Original Poster
Rep: Reputation: 15
thx but the file might contains a reference of word Sports anywhere so that command showed me all the files which ever contained the word sports irrespective of where the word is present.

the only unique reference is when sports is within <cats>

I just wanted to see the files which contains word withing <cats>





Quote:
Originally Posted by bigrigdriver View Post
Try this:
Code:
find /test/upload/ -type f -name "*.xml" -exec grep -l -A 1 -B 1 "Sports" {} \; -print
-A 1 print one line After the line containing Sports
-B 1 print one line Before the line containing Sports
 
Old 01-19-2010, 05:58 AM   #4
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726

Rep: Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706
Grep is usually used for single line matches. For multiline matching you may do better to look at sed.

Evo2.
 
Old 01-19-2010, 08:10 AM   #5
guessity
Member
 
Registered: Dec 2009
Posts: 41

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by evo2 View Post
Grep is usually used for single line matches. For multiline matching you may do better to look at sed.

Evo2.
thx. any idea how?
 
Old 01-19-2010, 08:22 AM   #6
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726

Rep: Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706
There are oodles of sed tutorials on the web. Have a google (or wait for a sed expert to post the anser here).

Cheers,

Evo2
 
Old 01-19-2010, 12:31 PM   #7
berbae
Member
 
Registered: Jul 2005
Location: France
Distribution: Arch Linux
Posts: 540

Rep: Reputation: Disabled
Code:
for file in $(find /test/upload/ -type f -name "*.xml" -print); do
    if $(sed -n '/<cats>/ {n
                           p}' $file|grep -q Sports); then
        echo $file
    fi
done
This seems to answer your request, if I understood what you want to get, ie only the name of the files with the pattern :
<cats> immediately followed on the next line by the word Sports.
 
Old 01-19-2010, 01:25 PM   #8
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,326

Rep: Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920Reputation: 920
maybe egrep -n "(sports|cats)" to get the line number.
then
if expr `grep -n sports` - `grep -n cats` -eq 1
then print the filename.

but there must be a simpler way.
 
Old 01-20-2010, 05:57 AM   #9
guessity
Member
 
Registered: Dec 2009
Posts: 41

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by berbae View Post
Code:
for file in $(find /test/upload/ -type f -name "*.xml" -print); do
    if $(sed -n '/<cats>/ {n
                           p}' $file|grep -q Sports); then
        echo $file
    fi
done
This seems to answer your request, if I understood what you want to get, ie only the name of the files with the pattern :
<cats> immediately followed on the next line by the word Sports.
works well. thx..
 
  


Reply


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
How to find all lines in a file that contains a particular word Coolsafe Linux - Newbie 6 11-14-2009 06:53 PM
Find multiple new lines char abhisheknayak Programming 3 12-17-2007 04:27 AM
find awk sed.. something along these lines citrus Linux - General 1 08-21-2006 04:04 PM
Help, help! How to find lines in between searched patterns? wujee Linux - Software 1 01-06-2005 03:52 PM
[c shell] How do I find how many lines a file has? saiz66 Programming 5 10-08-2004 04:01 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:31 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration