Reputation: 93
Ok so i can get a variable to pass into awk but when there is a space in variable it will not search anything so here is some code
new="bob"
searc=`awk '/#'$new':/,/---/ {print $0}'` file.txt
echo "$searc"
should display something like this
#bob:
happy
fun
love
sun
------
so the first example ^ works perfectly however Luke Jackson is never found now the reason for the name being stored in a variable is because it suppose to be dynamic so it can change. Now am i missing something simple here or i just cant do it this way?
new="Luke Jackson"
searc=`awk '/#'$new':/,/---/ {print $0}'` file.txt
echo "$searc"
#Luke Jackson:
sad
fun
evil
moon
------
Upvotes: 1
Views: 191
Reputation: 786289
Simple sed can also solve this:
new='Luke Jackson'
sed -n "/#$new:/,/---/p" file
#Luke Jackson:
sad
fun
evil
moon
------
Upvotes: 0
Reputation: 67567
if you rewrite your script slightly there won't be any issues
$ awk -v name='Luke Jackson' '$0~"#"name":"{f=1} f && /----/{f=0} f' file
#Luke Jackson:
sad
fun
evil
moon
note also that perhaps instead of pattern matching you need exact match, in that case use ==
Upvotes: 2