Strongly typed regexp constants is a handy tool that GNU Awk has. It is documented in GNU Awk User's Guide -> 6.1.2.2 Strongly Typed Regexp Constants and in there you can find interesting examples.
From reading it, and comments to an answer I made up some examples that show those:
$ cat file
he;llo
ho
are
you;
$ gawk -v patt='@/;/' '$0 ~ patt' file # it prints those lines containing ";"
he;llo
you;
In this case, we pass the pattern ";" with @/;/
and so it prints all the lines that contain ";" in them.
Now I wanted to go one step further and set this parameter dynamically. For example, by placing it on the first line of the file to read:
$ cat file
;
line2: hello;how
line3: are|you?
line4: i am;fine
However, I cannot manage to set the pattern to the string contained in $0, and I tried in various ways:
gawk 'NR==1 {f=@""$0; next} $0 ~ f' file
gawk 'NR==1 {f=@$0; next} $0 ~ f' file
But they all return a syntax error:
gawk: cmd. line:1: NR==1 {f=@$0; next} $0 ~ f
gawk: cmd. line:1: ^ syntax error
In this case, ";" is set as the pattern to match against and I would expect it to be processing the regexp from the 2nd line, and thus matching line 2 and 4, as if we would do gawk 'NR==1 {f=@/;/; next} $0 ~ f'
. However, I cannot set the strongly typed regexp constant dynamically.
Is there a way to do so?