0

I am trying to extract context from a file with awk from a start pattern until an end pattern.

Here is simplified sample input to reproduce:

$ cat file
1
2
3

4 5 6

When I pass the start pattern 1 as follows it works:

$ awk '/1/,/^$/' file

1 2 3

But when I pass a variable as the start pattern I get no result:

$ awk -v var=1 '/var/,/^$/' file

1 Answers1

4

Variable names are not expanded inside // in awk. So when you have /var/, awk will search for a v followed by an a and then an r. What you want is:

$  awk -v var=1 '$0~var,/^$/' file
1
2
3

terdon
  • 242,166