[SOLVED] search for 2 different strings in 2 diffrent lines
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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"
Distribution: Mainly Devuan with some Tiny Core, Fatdog, Haiku, & BSD thrown in.
Posts: 5,288
Rep:
Quote:
Originally Posted by threezerous
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"
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.
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 01:55 PM.
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"
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.