Issue
I need to use regex patterns in a grep pattern file (ERE) as BRE is insufficient. If data file contains:
alpha 1
bravo 1
gamma 1
delta 1
omicron 1
sigma 1
alpha 2
bravo 2
gamma 2
delta 2
and the grep inversion pattern file contains:
alpha
bravo
gamma
delta
then the following grep inversion:
$ grep -v -f pattern_file main_file
produces
omicron 1
sigma 1
This is the desired BRE output. But I need to tune the pattern file ERE so that the keywords stipulate beginning of line+keyword+space with the pattern file entries defined as:
"^alpha "
"^bravo "
"^gamma "
"^delta "
What is the correct grep inversion to produce the same two record output?
The following does not work:
$ grep -E -v -f pattern_file main_file
Solution
With the pattern file set as originally posted, a solution has been found that uses only a single grep inversion on an array loaded with the keywords from the main_file
. No additional *nix tools used.
Organizing ".bash_history" into ".bash_history.cmd" category files
declare -a keywords
keywords=(alpha bravo gamma delta)
IFS=\|; grep -E -v "^(${keywords[*]}) " main_file
which produces:
omicron 1
sigma 1
This satisfies the beginning of line anchor requirement as well as the space after the keyword, using an alternative token |
to account for {keywords[*]}
expansion, separated by the IFS character.
Answered By - Paul_Capo Answer Checked By - Clifford M. (WPSolving Volunteer)