0

I have a text file that have strings enclosed within double-quotes. I want to extract these strings and check another file to see if they exist or not. Right now, I got as far as:

awk -F'"' '{print $2}' ~/test | <GREP COMMAND>

How do I go through the extracted strings in a sort of a loop with my grep command?

3 Answers 3

2

Take a look at xargs, which reads lines from standard input and runs a given command once for each line. It'll depend on what version of xargs you have installed, but I would start with something like this:

awk -F'"' '{print $2}' ~/test | xargs -I % grep % <file>

The -I % flag tells it to replace % with the argument read from stdin, instead of appending it to the end of the commandline, which is the default.

2
  • This is fairly inefficient; it will spawn one grep for each search phrase.
    – tripleee
    Apr 3, 2012 at 19:15
  • Thanks this worked well for me: awk -F'"' '{print $2}' ~/test | xargs -I % grep 'name=\"%\"' file
    – syker
    Apr 3, 2012 at 19:55
1

If you have GNU grep;

grep -f - file

If not, you could print out a sed script from awk, or maybe create a slightly more complicated awk script. Or just join the output into one big egrep expression;

egrep "$(awk -F'"' '{ printf ("%s%s", j, $2); j="|" }' input)"
6
  • This solution was pretty cool too. Way more efficient than the spawn-a-grep-each-time.
    – syker
    Apr 3, 2012 at 19:58
  • In the printf command, why did j come before $2? I tested it out with $2 first and the my first and second strings were not pipe-delimited. I don't seem to see why $2 should not be first.
    – syker
    Apr 3, 2012 at 20:02
  • Also one other question, how does one surround the 'awk result with another string. Say I wanted to match against title="<STRING>" where <STRING> is the pipe-delimited result from "$(awk -F'"' '{ printf ("%s%s", j, $2); j="|" }' input)"
    – syker
    Apr 3, 2012 at 20:08
  • @syker Insert it in the awk output: $(awk -F'"' '{ printf ("%stitle=\"%s\"", j, $2); j="|" }' input)
    – blahdiblah
    Apr 3, 2012 at 21:24
  • Right, although it would be more efficient to only have title= once, like egrep 'title=(a|b|c)' rather than egrep 'title=a|title=b|title=c)'. You can factor that outside the awk script, like egrep 'title=('"$(awk ...)"')' file.
    – tripleee
    Apr 4, 2012 at 6:00
1

Unquote the strings and pipe to grep as a pattern file:

tr -d '"' < strings.txt | grep --file=- another_file.txt

(Note that this will not behave correctly with escaped " characters, but your awk command wouldn't either, so I assume it's not a problem.)

If applicable, using --fixed-strings with grep also will speed things up.

1
  • Ageee that tr -d '"' is way superior if removing leading and trailing double-quotes is all you needed awk for.
    – tripleee
    Apr 4, 2012 at 9:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.