3

I'm trying to read a file of regexes, looping over them and filtering them out of another file. I'm so close, but I'm having issues with my $regex var substitution I believe.

while read regex
do
  awk -vRS= '!/$regex/' ORS="\n\n" $tempOne > $tempTwo
  mv $tempTwo $tempOne
done < $filterFile

$tempOne and $tempTwo are temporary files. $filterFile is the file containing the regexes.

1
  • It looks like you want there to be a blank line where there used to be a line matching your regular expression? Is that why there's the ORS? Commented Mar 22, 2010 at 15:03

3 Answers 3

2

$regex is not getting expanded because it is single quoted. In bash, expansions are only done in doublequoted strings:

foo="bar"
echo '$foo'  # --> $foo
echo "$foo"  # --> bar

So, just break up your string like so:

'!'"/$regex/"

and it will behave as you expect. The ! should not be evaluated, since that will execute the last command in your history.

Sign up to request clarification or add additional context in comments.

Comments

2

pass your shell variable to awk using -v option

while read regex
do
  awk -vRS= -vregex="$regex" '$0!~regex' ORS="\n\n" $tempOne > $tempTwo
  mv $tempTwo $tempOne
done < $filterFile

Comments

0

I think you have a quoting problem

$ regex=asdf
$ echo '!/$regex/'
!/$regex/
$ echo "!/$regex/"
bash: !/$regex/: event not found
$ echo "\!/$regex/"
\!/asdf/
$ echo '!'"/$regex/"
!/asdf/

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.