How to pass BASH shell variables into AWK statement

陌路散爱 提交于 2020-01-05 07:29:22

问题


I want to pass 2 shell (bash) variables from into an awk statement. In this example the variables are marker1 and marker2

ubuntu@ubuntutest:/tmp$ echo $marker1
###___showhost___###
ubuntu@ubuntutest:/tmp$ echo $marker2
###___showhostset___###
ubuntu@ubuntutest:/tmp$

The script will extract all the lines of text between to different strings/markers ($marker1 and $marker2) from text file - file.in

ubuntu@ubuntutest:/tmp$ cat file.in 
###___showhost___###
 Id Name        Persona -WWN/iSCSI_Name- Port 
115 server5295 VMware  5005638024D3E8E6 1:2:4
116 server5296 VMware  5005638024D3EA86 1:2:4
117 server5297 VMware  5005638024D3F5D2 1:2:4
142 server5302 VMware  5005638024D3E8B6 1:2:4
143 server5303 VMware  5005638024D3E9C6 1:2:4
144 server5304 VMware  5005638024D3F4F2 1:2:4
###___showhostset___###
Id Name              Members    
 0 Mgt_Stack         server5295
 1 Cluster1          server5302
                     server5304
ubuntu@ubuntutest:/tmp$

If I run the awk statement, where I use the actual string values for variables marker1/marker2 ###___showhost___### and ###___showhostset___### respectively instead of using the variables themselves (within awk) I get a positive result. However, I want to put the awk statement in a bash scripts and need to read the variables marker1/marker2.

ubuntu@ubuntutest:/tmp$ awk '/^###___showhost___###/{flag=1;next}/^###___showhostset___###/{flag=0}flag' file.in 
 Id Name        Persona -WWN/iSCSI_Name- Port 
115 server5295 VMware  5005638024D3E8E6 1:2:4
116 server5296 VMware  5005638024D3EA86 1:2:4
117 server5297 VMware  5005638024D3F5D2 1:2:4
142 server5302 VMware  5005638024D3E8B6 1:2:4
143 server5303 VMware  5005638024D3E9C6 1:2:4
144 server5304 VMware  5005638024D3F4F2 1:2:4
ubuntu@ubuntutest:/tmp$

However, when i use the 2 variables $marker1 and $marker2 in the awk statement I get no output.

ubuntu@ubuntutest:/tmp$ awk '/^"'$marker1'"/{flag=1;next}/^"'$marker2'"/{flag=0}flag' file.in 
ubuntu@ubuntutest:/tmp$

Any help greatly appreciated !!!


回答1:


You have to use -v, Getting shell variables into awk may be done in several ways. Some are better than others.

This is the best way to do it (Please note use a space after -v or it will be less portable. E.g., awk -v var= not awk -vvar)

# your shell variables
marker1="###___showhost___###"
marker2="###___showhostset___###"

# and passing them to awk
awk -v market1="$market1" -v market2="$market2" '
       $0 ~ "^"market1{found=1;next}
       $0 ~ "^"market2{found=0}found' file.in

Example:

akshay@db-3325:~$ foo="this is my shell variable"
akshay@db-3325:~$ awk -v myvar="$foo" 'BEGIN{print myvar}'
this is my shell variable



回答2:


You may pass command line arguments to you awk script. There are built-in variables ARGC and ARGV which can be used for access:

$ marker1=###___showhost___###

$ echo $marker1
###___showhost___###

$ awk 'BEGIN { marker1 = ARGV[1] ; print marker1 }' $marker1
###___showhost___###

I was a little bit suspicious about all these hashmarks (#) because they start comments in bash scripts but seem not to be a problem on command line.

Edit:

This can be a problem in awk too. The answer of Akshay Hedge handles this in a simple way.



来源:https://stackoverflow.com/questions/42814380/how-to-pass-bash-shell-variables-into-awk-statement

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!