bash - Pass shell parameters to awk regex -
i have following script
#!/bin/bash file="path/to/file" /usr/bin/awk '/chapter:/ {f=0} /chapter: 25/ {f=1} f' $file
how can use variable's value instead of 25
?
something this:
#!/bin/bash file="path/to/file" num=25 /usr/bin/awk '/chapter:/ {f=0} /chapter: <num>/ {f=1} f' $file
p.s.
/usr/bin/awk -v n="$num" '/chapter:/ {f=0} /chapter: n/ {f=1} f' $file
is not working- i cannot use other tool that, because
awk
fastest 1 want o do
any ideas?
you're on right track here 2nd part of building regex. can use:
awk -v n="$num" '/chapter:/{f=0} $0 ~ "chapter: " n {f=1} f' file
you need build regex using variable n
, use ~
operator regex matching.
Comments
Post a Comment