如何将BASH Shell变量传递到AWK语句 [英] How to pass BASH shell variables into AWK statement

查看:127
本文介绍了如何将BASH Shell变量传递到AWK语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将2个shell(bash)变量从传递到awk语句中. 在此示例中,变量为marker1和marker2

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$

该脚本将从文本文件-file.in中提取到不同字符串/标记($ marker1和$ marker2)之间的所有文本行.

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$

如果运行awk语句,则将实际字符串值用于变量marker1/marker2 ### ___ showhost ___ ### ### ___ showhostset ___ ### >而不是使用变量本身(在awk内),我得到了肯定的结果.但是,我想将awk语句放在bash脚本中,并且需要读取变量marker1/marker2.

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$

但是,当我在awk语句中使用2个变量$ marker1和$ marker2时,我没有输出.

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 !!!

推荐答案

您必须使用-v,将Shell变量放入awk的方法可能有多种.有些比其他更好.

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

这是执行此操作的最佳方法(请注意,在-v之后使用空格,否则它将不太便于使用.例如,awk -v var=而不是awk -vvar)

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

示例:

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

这篇关于如何将BASH Shell变量传递到AWK语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆