1

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.

user47812
  • 33
  • 5

1 Answers1

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
DopeGhoti
  • 76,081