Issue
I have a file with a simple string on each line...
bob
fred
sally
I want to use each string as part of a regex
grep -r "regex using <bob>" (value from line 1)
grep -r "regex using <fred>" (value from line 2
grep -r "regex using <sally>" (value from line 3)
I expect to use the file strings in various regex's so not wanting to edit original file. Preferably just using pipes, or creating a temp file if that's a must.
Solution
sed 's/.*/regex using <&>/' file-with-strings | grep -f- file-to-search
Basically use sed to wrap each line of file-with-strings in your regex, then pipe it to grep -f-
which will read regexes from stdin
Or:
grep -P "regex using <$(tr '\n' '|' file-with-strings)>" file-to-search
... which will do what you want in the comments: assemble bob|fred|sally
and wrap it in your regex. Requires bash or similar for $()
syntax.
Answered By - stevesliva Answer Checked By - Mary Flores (WPSolving Volunteer)