UriE
UriE

Reputation: 93

How to extract string between two regexes passed as variables to awk?

I'm trying to extract some text in a passage that is between two strings that are being saved in variables. What is wrong with the following way?

input:

module dft ( a, b, c, clk, z, test_si, test_se );

input [7:0] a;

dft_DW_mult_uns_1 mult_31 ( .a(a), .b(b), .product(reg0) );

endmodule

output:

input [7:0] a;

dft_DW_mult_uns_1 mult_31 ( .a(a), .b(b), .product(reg0) );

using the following code:

try="module dft";

awk '/$try/{flag=1; next} /endmodule/{flag=0} flag' dft_syn.v

but it doesn't recognize the $try variable.

Upvotes: 3

Views: 117

Answers (1)

anubhava
anubhava

Reputation: 784898

You can use awk like this:

sm="module dft"
em="endmodule"

awk -v sm="$sm" -v em="$em" '$0 ~ em{p=0} p; $0 ~ sm{p=1}' file

...which property emits as output:

input [7:0] a;

dft_DW_mult_uns_1 mult_31 ( .a(a), .b(b), .product(reg0) );
  • Use -v var="value" to pass command line variables from shell to awk
  • Set and reset variable p when you encounter start or end tags

Upvotes: 2

Related Questions