如何使用awk脚本shell变量 [英] How to use shell variables in awk script

查看:234
本文介绍了如何使用awk脚本shell变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了一些方法来传递外部shell变量到awk脚本,但我很困惑和

I found some ways to pass external shell variables to the awk script,but I'm confused about ' and ".

首先,我输入的shell脚本:

Firstly I inputted the shell script:

$ v=123test
$ echo $v
123test
$ echo "$v"
123test

然后我输入的awk脚本:

Then I inputted the awk script:

$ awk 'BEGIN{print "'$v'"}'
$ 123test
$ awk 'BEGIN{print '"$v"'}'
$ 123

为什么?..

最后我想是这样的:

$ awk 'BEGIN{print " '$v' "}'
$  123test
$ awk 'BEGIN{print ' "$v" '}'
awk: cmd. line:1: BEGIN{print
awk: cmd. line:1:             ^ unexpected newline or end of string 

我很困惑这个..

I'm confused about this..

推荐答案

获取shell变量到 AWK 可通过多种方式进行。有些人比别人做得更好。

Getting shell variables into awk may be done in several ways. Some are better than others.

这是做到这一点的最好办法。它使用 -v 选项:(PS后使用 -v 空格或就不会那么便携例如, 的awk -v VAR = 不是 AWK -vvar

This is the best way to do it. It uses the -v option: (P.S. use a space after -v or it will be less portable. E.g., awk -v var= not awk -vvar)

variable="line one\nline two"
awk -v var="$variable" 'BEGIN {print var}'
line one
line two

这应该是兼容大部分 AWK 和变量在 BEGIN 块以及可供选择:

This should be compatible with most awk and variable is available in the BEGIN block as well:

您可以使用 AWK code中的一个变量,但它的混乱和难以阅读,并为查尔斯·达菲指出,这个版本也可能是code注射液的受害者。如果有人将不好的东西的变量,它会随着 AWK code的一部分来执行的,所以不要使用。

You can use a variable within the awk code, but it's messy and hard to read, and as Charles Duffy points out, this version may also be a victim of code injection. If someone adds bad stuff to the variable, it will be executed as part of the awk code, so DO NOT USE.

variable="line one\nline two"
awk 'BEGIN {print "'"$variable"'"}'
line one
line two

下面是code注射液的例子:

Here is an example of code injection:

variable='line one\nline two" ; for (i=1;i<=1000;++i) print i"'
awk 'BEGIN {print "'"$variable"'"}'
line one
line two
1
2
3
.
.
1000

您可以添加大量的命令来 AWK 这种方式。即使使其与非有效的命令崩溃。

You can add lots of commands to awk this way. Even make it crash with non valid commands.

在这里,我们得到了 AWK code后的变量。这只要做工精细,你不必在变量中的 BEGIN 块:

Here we get the variable after the awk code. This will work fine as long as you do not need the variable in the BEGIN block:

variable="line one\nline two"
echo "input data" | awk '{print var}' var="$variable"
or
awk '{print var}' var="$variable" file


变量也可以加入到 AWK 在这里使用

awk '{print $0}' <<< "$variable"
test

这是相同的:

echo "$variable" | awk '{print $0}'


这是一件好事,双引号变量$变量结果
如果不是,多线将被加入作为长单行


It's always good to double quote variable "$variable"
If not, multiple lines will be added as a long single line.

例如:

var="Line one
This is line two"

echo $var
Line one This is line two

echo "$var"
Line one
This is line two

您可以得到其他错误,而双引号:

Other errors you can get without double quote:

variable="line one\nline two"
awk -v var=$variable 'BEGIN {print var}'
awk: cmd. line:1: one\nline
awk: cmd. line:1:    ^ backslash not last character on line
awk: cmd. line:1: one\nline
awk: cmd. line:1:    ^ syntax error

和用单引号,它不扩大变量的值:

And with single quote, it does not expand the value of the variable:

awk -v var='$variable' 'BEGIN {print var}'
$variable

这篇关于如何使用awk脚本shell变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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