Pulling grep patterns from a file

fleance

New Member
Credits
0
Hello out there, I want to pull out the patterns which I feed grep out of file. The file has the form name=value and I want read out the values one by one in order to give it to grep. The name of the file is executables and it's in my homedir.
My attempt was
Bash:
while read line; do grep -nr $(awk 'BEGIN {FS="="}; {print $2}'); done < ~/executables
but the result was, that grep couldn't find any of the patterns within the file. But if I try this manually there are some matches. Most probably grep receives all the matches at one time.
Does anybody know, what my fault is?

Thank you in advance

BR Fleance
 


rdrtx1

New Member
Credits
0
Code:
awk -F= '!a[$2]++ {print $2}' ~/executables | while read var
do
     grep "\b$var\b" file_to_search
done
 
Last edited:

JasKinasis

Well-Known Member
Credits
1,193
Hello out there, I want to pull out the patterns which I feed grep out of file. The file has the form name=value and I want read out the values one by one in order to give it to grep. The name of the file is executables and it's in my homedir.
My attempt was
Bash:
while read line; do grep -nr $(awk 'BEGIN {FS="="}; {print $2}'); done < ~/executables
but the result was, that grep couldn't find any of the patterns within the file. But if I try this manually there are some matches. Most probably grep receives all the matches at one time.
Does anybody know, what my fault is?

Thank you in advance

BR Fleance
@fleance - For starters - in your grep statement - you haven't specified a file to search in. That might be a large part of the problem.
You've specified the pattern - the result of the awk - But no search path/file.
Also, I'm not sure whether the awk will work in there like that. Would awk implicitly know to operate on the current line, read in from the file?.... IDK offhand.

Personally - I'd do something similar to @rdrtx1 and process the file in awk outside the loop and then set up a loop to catch and process each piped line of output.

But I don't quite follow what @rdrtx1's awk is doing, so I've gone with your original pattern, which looks fine to me!
Bash:
awk 'BEGIN {FS="="}{print $2}' ~/executables | while read var ; do
    echo "Searching for $var:"
    grep -nr "$var" /path/to/search
    echo
done
Where /path/to/search is the path or file to search inside.

That way the search terms can be read in from the executables file and it will search /path/to/search for matches. And the echo statements will format the output nicely so you can see which terms were used in the searches and any subsequent results.
 

rdrtx1

New Member
Credits
0
The awk is doing as stated in #1 post: "read out the values one by one in order to give it to grep"

The solution on #3 post could return repeat values from the awk (and perform more than one grep for a value) and will also match values like "value21" when searching for "value2".
 
Last edited:


Members online


Latest posts

Top