1

This question already has an answer here:

I realize this has been asked many times and many of the answers seam well written, but I'm missing something with my little task. Trying to pass a Shell variable (an argument in this case) to awk to grab a section of a text file out.

cat ~/work/junk.txt 

[Section1]
innerline1
innerline2
innerline3

[Section2]
innerline4
innerline5
innerline6

I'm trying to make a script to use the provided argument ($1) to pass as the pattern to match in awk.

#!/bin/bash

echo pattern is $1

# record separator (RS) is the next blank line
awk -v var="$1" {print '/var/'} RS= ~/work/junk.txt

If $1 is the argument provided as "Section1" I expect this output:

[Section1]
innerline1
innerline2
innerline3

I think I'm close, but yet so very far.

Thanks for any ideas.

marked as duplicate by jasonwryan, HalosGhost, Jeff Schaller, sam, GAD3R Nov 23 '16 at 20:02

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2
#!/bin/bash

echo "Pattern is ${1}"

awk -F'=' -v section="[$1]" -v f=0 '$0==section {f=1}  $0=="" {f=0} f==1 {print}' input

Not the answer you're looking for? Browse other questions tagged or ask your own question.