Use regex command in awk in bash script

I am trying to read a regular expression file, iterate over them and filter from another file. I am so close, but I have problems with my $ regex var variable. I suppose.

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 a file containing regular expressions.

+3
source share
3 answers

$regexdoes not expand as it is single. In bash, decompositions are performed only in double lines:

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

So just fold your line like this:

'!'"/$regex/"

and he will behave as you expect. !should not be evaluated, as this will execute the last command in your story.

+2
source

awk -v

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

,

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

Source: https://habr.com/ru/post/1738043/


All Articles