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 07-30-2012, 06:56 AM   #1
threezerous
Member
 
Registered: Jul 2009
Posts: 96

Rep: Reputation: 16
search for 2 different strings in 2 diffrent lines


I would like to search for 2 different strings on 2 different lines in files in recursive directories. Both these strings must appear at the beginning of the line. I was able to write the script for searching for one string as follows:
The string I searched for was "</string1-abc"

egrep -ilr '^</string1-abc' ./* > /archive/files_with_broken_links.txt &

On the output results I would like to search for all files which have the string "string2-xyz" at the beginning of another line.

Any suggestions/guidance are appreciated much and thanks in advance.
 
Old 07-30-2012, 07:18 AM   #2
fatmac
LQ Guru
 
Registered: Sep 2011
Location: Upper Hale, Surrey/Hants Border, UK
Distribution: One main distro, & some smaller ones casually.
Posts: 5,862

Rep: Reputation: Disabled
Quote:
Originally Posted by threezerous View Post
I would like to search for 2 different strings on 2 different lines in files in recursive directories. Both these strings must appear at the beginning of the line. I was able to write the script for searching for one string as follows:
The string I searched for was "</string1-abc"

egrep -ilr '^</string1-abc' ./* > /archive/files_with_broken_links.txt &

On the output results I would like to search for all files which have the string "string2-xyz" at the beginning of another line.

Any suggestions/guidance are appreciated much and thanks in advance.
Would not this give you what you want?

egrep -ilr '^</string1-abc' ./* >> /archive/files_with_broken_links.txt &
egrep -ilr '^</string2-xyz' ./* >> /archive/files_with_broken_links.txt &
 
Old 07-30-2012, 09:24 AM   #3
threezerous
Member
 
Registered: Jul 2009
Posts: 96

Original Poster
Rep: Reputation: 16
I guess that would give me two lists, each listing the files having the string grepped for. I would like to have an AND condition. Files having string1-abc AND string2-xyz in the same file.

Thanks
 
Old 07-30-2012, 12:40 PM   #4
fatmac
LQ Guru
 
Registered: Sep 2011
Location: Upper Hale, Surrey/Hants Border, UK
Distribution: One main distro, & some smaller ones casually.
Posts: 5,862

Rep: Reputation: Disabled
As written, both searches are appended to the same file, because of the use of the redirector >> instead of >.

First listed would be all your first search string followed by all your second search string.
 
Old 07-30-2012, 12:54 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Your requirements are rather unclear.

Please post an actual example of the input text, exactly what needs to be matched in it, and how the output needs to be presented. Also explain what kinds of variations we could expect. Does one line always come before another, or can they be reversed? Do they follow one another directly or can they be separated by other text? Are they found randomly in the file or only in certain relationships to other text? Etc.

The best solution to use usually depends on first defining the exact patterns in the input that need to be matched. Both the text to match and the text to exclude need to be taken into account.

Finally, this looks like html or xml. Regex-based tools are not well-suited for working with these freeform data types. It may be better for you to use a tool with a dedicated parser. Again, it will help if you explain your exact requirements.

Last edited by David the H.; 07-30-2012 at 12:55 PM.
 
Old 07-30-2012, 01:20 PM   #6
threezerous
Member
 
Registered: Jul 2009
Posts: 96

Original Poster
Rep: Reputation: 16
Actually I was able to resolve the issue by running the following script on the output file of the first command.

#!/bin/bash
# To execute ./grep.sh &

INPUT_FILE="broken_links.txt"
OUT_RESULT_FILE="broken_links_string2.txt"


echo "">"$OUT_RESULT_FILE"

while read line
do
if egrep '<string2-xyz>' "$line"
then
/bin/echo "$line" >> "$OUT_RESULT_FILE"
else
/bin/echo "NOT found in $line"
fi
done < "$INPUT_FILE"

Thanks all for your suggestions and comments.
 
Old 07-30-2012, 02:51 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Well, congratulations on figuring out a solution on your own.

But it's a rather clunky one, having to run two separate filtering operations. Again, if you'd provide some details we'd almost certainly be able to work up something cleaner.


And please use ***[code][/code] tags*** around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, bolding, colors, or other fancy formatting.
 
Old 07-30-2012, 02:56 PM   #8
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594

Rep: Reputation: 141Reputation: 141
This doesn't search through the subdirs then you have to use find bla bla exec grep etc
Code:
bash-4.2$ for f in *; do grep -q "^</string1-abc" $f && grep -l "^</string2-abc" $f; done > output.txt
 
Old 07-30-2012, 03:42 PM   #9
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594

Rep: Reputation: 141Reputation: 141
How can I use this
Code:
bash-4.2$ for f in *; do grep -q "^</string1-abc" $f && grep -l "^</string2-abc" $f; done > output.txt
with find I tried with exec and exec sh -c but it doesn't work.
Code:
find . -type 'f' -exec sh -c 'grep -l "^</string1-abc" {} && grep -l "^</string2-abc"' {} \;
 
  


Reply

Tags
egrep, grep, search


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
truncate strings on many lines mufea Linux - Newbie 2 02-23-2012 06:29 AM
fastest way to search for strings niteshadw Linux - Newbie 4 01-30-2010 06:24 AM
How to remove lines and parts of lines from python strings? golmschenk Programming 3 11-26-2009 11:29 PM
Awk Question to search specific strings grouped by blank lines rk4k Programming 6 07-07-2008 11:56 PM
How to search for text strings? armandino LQ Suggestions & Feedback 2 06-24-2007 07:51 PM

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

All times are GMT -5. The time now is 03:20 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