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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
07-30-2012, 06:56 AM
|
#1
|
Member
Registered: Jul 2009
Posts: 96
Rep:
|
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.
|
|
|
07-30-2012, 07:18 AM
|
#2
|
LQ Guru
Registered: Sep 2011
Location: Upper Hale, Surrey/Hants Border, UK
Distribution: One main distro, & some smaller ones casually.
Posts: 5,862
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"
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 &
|
|
|
07-30-2012, 09:24 AM
|
#3
|
Member
Registered: Jul 2009
Posts: 96
Original Poster
Rep:
|
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
|
|
|
07-30-2012, 12:40 PM
|
#4
|
LQ Guru
Registered: Sep 2011
Location: Upper Hale, Surrey/Hants Border, UK
Distribution: One main distro, & some smaller ones casually.
Posts: 5,862
Rep: 
|
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.
|
|
|
07-30-2012, 12:54 PM
|
#5
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
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.
|
|
|
07-30-2012, 01:20 PM
|
#6
|
Member
Registered: Jul 2009
Posts: 96
Original Poster
Rep:
|
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.
|
|
|
07-30-2012, 02:51 PM
|
#7
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
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.
|
|
|
07-30-2012, 02:56 PM
|
#8
|
Member
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594
Rep: 
|
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
|
|
|
07-30-2012, 03:42 PM
|
#9
|
Member
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594
Rep: 
|
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"' {} \;
|
|
|
All times are GMT -5. The time now is 03:20 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|