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.

  1. /usr/bin/awk -v n="$num" '/chapter:/ {f=0} /chapter: n/ {f=1} f' $fileis not working
  2. 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

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -