Can we use shell variables in AWK like $VAR
instead of $1
, $2
? For example:
我们可以使用AWK中的shell变量$VAR而不是$1,$2吗?例如:
UL=(AKHIL:AKHIL_NEW,SWATHI:SWATHI_NEW)
NUSR=`echo ${UL[*]}|awk -F, '{print NF}'`
echo $NUSR
echo ${UL[*]}|awk -F, '{print $NUSR}'
Actually am an oracle DBA we get lot of import requests. I'm trying to automate it using the script. The script will find out the users in the dump and prompt for the users to which dump needs to be loaded.
实际上我是一个oracle DBA,我们收到很多导入请求。我试着用脚本实现自动化。该脚本将在转储文件中找到用户,并提示需要加载转储的用户。
Suppose the dumps has two users AKHIL
, SWATHI
(there can be may users in the dump and i want to import more number of users). I want to import the dumps to new users AKHIL_NEW
and SWATHI_NEW
. So the input to be read some think like AKHIL:AKHIL_NEW,SWATHI:SWATHI_NEW
.
假设转储有两个用户AKHIL, SWATHI(在转储中可以有可能的用户,我想要导入更多的用户)。我想将转储导入到新用户AKHIL_NEW和SWATHI_NEW。因此,输入要读一些像AKHIL:AKHIL_NEW,SWATHI:SWATHI_NEW。
First, I need to find the Number of users to be created, then I need to get new users i.e. AKHIL_NEW,SWATHI_NEW
from the input we have given. So that I can connect to the database and create the new users and then import. I'm not copying the entire code: I just copied the code from where it accepts the input users.
首先,我需要找到要创建的用户的数量,然后我需要得到新的用户,即AKHIL_NEW,SWATHI_NEW,从我们给出的输入。这样我就可以连接到数据库并创建新用户,然后导入。我并没有复制整个代码:我只是从它接受输入用户的地方复制代码。
UL=(AKHIL:AKHIL_NEW,SWATHI:SWATHI_NEW) ## it can be many users like USER1:USER1_NEW,USER2_USER2_NEW,USER3:USER_NEW..
NUSR=`echo ${UL[*]}|awk -F, '{print NF}'` #finding number of fields or users
y=1
while [ $y -le $NUSR ] ; do
USER=`echo ${UL[*]}|awk -F, -v NUSR=$y '{print $NUSR}' |awk -F: '{print $2}'` #getting Users to created AKHIL_NEW and SWATHI_NEW and passing to SQLPLUS
if [[ $USER = SCPO* ]]; then
TBS=SCPODATA
else
if [[ $USER = WWF* ]]; then
TBS=WWFDATA
else
if [[ $USER = STSC* ]]; then
TBS=SCPODATA
else
if [[ $USER = CSM* ]]; then
TBS=CSMDATA
else
if [[ $USER = TMM* ]]; then
TBS=TMDATA
else
if [[ $USER = IGP* ]]; then
TBS=IGPDATA
fi
fi
fi
fi
fi
fi
sqlplus -s '/ as sysdba' <<EOF # CREATING the USERS in the database
CREATE USER $USER IDENTIFIED BY $USER DEFAULT TABLESPACE $TBS TEMPORARY TABLESPACE TEMP QUOTA 0K on SYSTEM QUOTA UNLIMITED ON $TBS;
GRANT
CONNECT,
CREATE TABLE,
CREATE VIEW,
CREATE SYNONYM,
CREATE SEQUENCE,
CREATE DATABASE LINK,
RESOURCE,
SELECT_CATALOG_ROLE
to $USER;
EOF
y=`expr $y + 1`
done
impdp sysem/manager DIRECTORY=DATA_PUMP DUMPFILE=imp.dp logfile=impdp.log SCHEMAS=AKHIL,SWATHI REMPA_SCHEMA=${UL[*]}
In the last impdp command I need to get the original users in the dumps i.e AKHIL,SWATHI using the variables.
在最后一个impdp命令中,我需要让原始用户处于转储I。e AKHIL,SWATHI使用变量。
6 个解决方案
#1
48
Yes, you can use the shell variables inside awk. There are a bunch of ways of doing it, but my favorite is to define a variable with the -v
flag:
是的,您可以在awk中使用shell变量。有很多方法,但我最喜欢的是用-v标记定义一个变量:
$ echo | awk -v my_var=4 '{print "My var is " my_var}'
My var is 4
Just pass the environment variable as parameter to the -v
flag. For example, if you have this variable:
将环境变量作为参数传递给-v标志。例如,如果你有这个变量:
$ VAR=3
$ echo $VAR
3
Use it this way:
这样使用它:
$ echo | awk -v env_var="$VAR" '{print "The value of VAR is " env_var}'
The value of VAR is 3
Of course, you can give the same name, but the $
will not be necessary:
当然,您可以提供相同的名称,但是$将不需要:
$ echo | awk -v VAR="$VAR" '{print "The value of VAR is " VAR}'
The value of VAR is 3
A note about the $
in awk: unlike bash, Perl, PHP etc. it is not part of the name of the variable but rather an operator.
与bash、Perl、PHP等不同的是,它不是变量名称的一部分,而是一个操作符。
#2
12
Awk and Gawk provide the ENVIRON associative array that holds all exported environment variables. So in your awk script you can use ENVIRON["VarName"] to get the value of VarName, provided that VarName has been exported before running awk.
Awk和Gawk提供了保存所有导出环境变量的环境关联数组。因此,在awk脚本中,您可以使用环境[“VarName”]来获取VarName的值,前提是VarName在运行awk之前已经导出。
Note ENVIRON is a predefined awk variable NOT a shell environment variable.
注意环境是一个预定义的awk变量,而不是shell环境变量。
Since I don't have enough reputation to comment on the other answers I have to include them here!
因为我没有足够的声誉来评论其他的答案,所以我必须把它们包括在这里!
The earlier answer showing $ENVIRON is incorrect - that syntax would be expanded by the shell, and probably result in expanding to nothing.
前面的答案显示$环境是不正确的——语法会被shell扩展,可能导致扩展为零。
Further earlier comments about C not being able to access environment variable is wrong. Contrary to what is said above, C (and C++) can access environment variables using the getenv("VarName") function. Many other languages provide similar access (e.g., Java: System.getenv(), Python: os.environ, Haskell System.Environment, ...). Note in all cases access to environment variables is read-only, you cannot change an environment variable in a program and get that value back to the calling script.
之前关于C不能访问环境变量的评论是错误的。与上面所说的相反,C(和c++)可以使用getenv(“VarName”)函数访问环境变量。许多其他语言也提供类似的访问(例如,Java: System.getenv(), Python: os)。环境,Haskell系统。环境,…)。注意,在所有情况下,对环境变量的访问都是只读的,您不能在程序中更改环境变量,并将该值返回给调用脚本。
#3
12
There are two ways to pass variables to awk
: one way is defining the variable in a command line argument:
有两种方法将变量传递给awk:一种方法是在命令行参数中定义变量:
$ echo ${UL[*]}|awk -F, -v NUSR=$NUSR '{print $NUSR}'
SWATHI:SWATHI_NEW
Another way is converting the shell variable to an environment variable using export
, and reading the environment variable from the ENVIRON
array:
另一种方法是使用导出将shell变量转换为环境变量,并从环境数组中读取环境变量:
$ export NUSR
$ echo ${UL[*]}|awk -F, '{print $ENVIRON["NUSR"]}'
SWATHI:SWATHI_NEW
Update 2016: The OP has comma-separated data and wants to extract an item given its index. The index is in the shell variable NUSR
. The value of NUSR
is passed to awk
, and awk
's dollar operator extracts the item.
更新2016:OP有逗号分隔的数据,并希望提取给定索引的项。索引在shell变量NUSR中。NUSR的值被传递给awk, awk的美元操作员提取该项目。
Note that it would be simpler to declare UL as an array of more than one element, and do the extraction in bash
, and take awk
out of the equation completely. This however uses 0-based indexing.
请注意,将UL声明为多个元素的数组,并在bash中进行提取,并将awk完全从这个等式中提取出来,将会更简单。但是,这使用了基于0的索引。
UL=(AKHIL:AKHIL_NEW SWATHI:SWATHI_NEW)
NUSR=1
echo ${UL[NUSR]} # prints SWATHI:SWATHI_NEW
#4
3
There is another way, but it could cause immense confusion:
有另一种方式,但它可能造成巨大的混乱:
$ VarName="howdy" ; echo | awk '{print "Just saying '$VarName'"}'
Just saying howdy
$
So you are temporarily exiting the single quote environment (which would normally prevent the shell from interpreting '$') to interpret the variable and then going back into it. It has the virtue of being relatively brief.
因此,您暂时退出单引号环境(这通常会阻止shell解释“$”)来解释变量,然后返回到它。它具有相对简洁的优点。
#5
2
Not sure if i understand your question.
不知道我是否理解你的问题。
But lets say we got a variable number=3
and we want to use it istead of $3
, in awk we can do that with the following code
但是假设我们有一个变量number=3,我们想用它代替3美元,在awk中我们可以用下面的代码来做。
results="100 Mbits/sec 110 Mbits/sec 90 Mbits/sec"
number=3
speed=$(echo $results | awk '{print '"\$${number}"'}')
so the speed variable will get the value 110.
所以速度变量会得到110。
Hope this helps.
希望这个有帮助。
#6
-1
No. You can pass the value of a shell variable to an awk script just like you can pass the value of a shell variable to a C program but you cannot access a shell variable in an awk script any more than you could access a shell variable in a C program. Like C, awk is not shell. See question 24 in the comp.unix.shell FAQ at cfajohnson.com/shell/cus-faq-2.html#Q24.
不。您可以将一个shell变量的值传递给一个awk脚本就像你可以将一个shell变量的值传递给一个C程序,但你不能访问shell变量在awk脚本任何超过你可以访问一个shell变量C程序。像C, awk不是shell。在unix中看到问题24。壳牌FAQ cfajohnson.com/shell/cus -常见问题- 2. - html #抓起。
One way to write your code would be:
编写代码的一种方法是:
UL="AKHIL:AKHIL_NEW,SWATHI:SWATHI_NEW"
NUSR=$(awk -F, -v ul="$UL" 'BEGIN{print gsub(FS,""); exit}')
echo "$NUSR"
echo "$UL" | awk -F, -v nusr="$NUSR" '{print $nusr}' # could have just done print $NF
but since your original starting point:
但因为你的出发点:
UL=(AKHIL:AKHIL_NEW,SWATHI:SWATHI_NEW)
was declaring UL as an array with just one entry, you might want to rethink whatever it is you're trying to do as you may have completely the wrong approach.
将UL声明为一个只有一个条目的数组,您可能想要重新考虑您想要做什么,因为您可能完全错误的方法。
#1
48
Yes, you can use the shell variables inside awk. There are a bunch of ways of doing it, but my favorite is to define a variable with the -v
flag:
是的,您可以在awk中使用shell变量。有很多方法,但我最喜欢的是用-v标记定义一个变量:
$ echo | awk -v my_var=4 '{print "My var is " my_var}'
My var is 4
Just pass the environment variable as parameter to the -v
flag. For example, if you have this variable:
将环境变量作为参数传递给-v标志。例如,如果你有这个变量:
$ VAR=3
$ echo $VAR
3
Use it this way:
这样使用它:
$ echo | awk -v env_var="$VAR" '{print "The value of VAR is " env_var}'
The value of VAR is 3
Of course, you can give the same name, but the $
will not be necessary:
当然,您可以提供相同的名称,但是$将不需要:
$ echo | awk -v VAR="$VAR" '{print "The value of VAR is " VAR}'
The value of VAR is 3
A note about the $
in awk: unlike bash, Perl, PHP etc. it is not part of the name of the variable but rather an operator.
与bash、Perl、PHP等不同的是,它不是变量名称的一部分,而是一个操作符。
#2
12
Awk and Gawk provide the ENVIRON associative array that holds all exported environment variables. So in your awk script you can use ENVIRON["VarName"] to get the value of VarName, provided that VarName has been exported before running awk.
Awk和Gawk提供了保存所有导出环境变量的环境关联数组。因此,在awk脚本中,您可以使用环境[“VarName”]来获取VarName的值,前提是VarName在运行awk之前已经导出。
Note ENVIRON is a predefined awk variable NOT a shell environment variable.
注意环境是一个预定义的awk变量,而不是shell环境变量。
Since I don't have enough reputation to comment on the other answers I have to include them here!
因为我没有足够的声誉来评论其他的答案,所以我必须把它们包括在这里!
The earlier answer showing $ENVIRON is incorrect - that syntax would be expanded by the shell, and probably result in expanding to nothing.
前面的答案显示$环境是不正确的——语法会被shell扩展,可能导致扩展为零。
Further earlier comments about C not being able to access environment variable is wrong. Contrary to what is said above, C (and C++) can access environment variables using the getenv("VarName") function. Many other languages provide similar access (e.g., Java: System.getenv(), Python: os.environ, Haskell System.Environment, ...). Note in all cases access to environment variables is read-only, you cannot change an environment variable in a program and get that value back to the calling script.
之前关于C不能访问环境变量的评论是错误的。与上面所说的相反,C(和c++)可以使用getenv(“VarName”)函数访问环境变量。许多其他语言也提供类似的访问(例如,Java: System.getenv(), Python: os)。环境,Haskell系统。环境,…)。注意,在所有情况下,对环境变量的访问都是只读的,您不能在程序中更改环境变量,并将该值返回给调用脚本。
#3
12
There are two ways to pass variables to awk
: one way is defining the variable in a command line argument:
有两种方法将变量传递给awk:一种方法是在命令行参数中定义变量:
$ echo ${UL[*]}|awk -F, -v NUSR=$NUSR '{print $NUSR}'
SWATHI:SWATHI_NEW
Another way is converting the shell variable to an environment variable using export
, and reading the environment variable from the ENVIRON
array:
另一种方法是使用导出将shell变量转换为环境变量,并从环境数组中读取环境变量:
$ export NUSR
$ echo ${UL[*]}|awk -F, '{print $ENVIRON["NUSR"]}'
SWATHI:SWATHI_NEW
Update 2016: The OP has comma-separated data and wants to extract an item given its index. The index is in the shell variable NUSR
. The value of NUSR
is passed to awk
, and awk
's dollar operator extracts the item.
更新2016:OP有逗号分隔的数据,并希望提取给定索引的项。索引在shell变量NUSR中。NUSR的值被传递给awk, awk的美元操作员提取该项目。
Note that it would be simpler to declare UL as an array of more than one element, and do the extraction in bash
, and take awk
out of the equation completely. This however uses 0-based indexing.
请注意,将UL声明为多个元素的数组,并在bash中进行提取,并将awk完全从这个等式中提取出来,将会更简单。但是,这使用了基于0的索引。
UL=(AKHIL:AKHIL_NEW SWATHI:SWATHI_NEW)
NUSR=1
echo ${UL[NUSR]} # prints SWATHI:SWATHI_NEW
#4
3
There is another way, but it could cause immense confusion:
有另一种方式,但它可能造成巨大的混乱:
$ VarName="howdy" ; echo | awk '{print "Just saying '$VarName'"}'
Just saying howdy
$
So you are temporarily exiting the single quote environment (which would normally prevent the shell from interpreting '$') to interpret the variable and then going back into it. It has the virtue of being relatively brief.
因此,您暂时退出单引号环境(这通常会阻止shell解释“$”)来解释变量,然后返回到它。它具有相对简洁的优点。
#5
2
Not sure if i understand your question.
不知道我是否理解你的问题。
But lets say we got a variable number=3
and we want to use it istead of $3
, in awk we can do that with the following code
但是假设我们有一个变量number=3,我们想用它代替3美元,在awk中我们可以用下面的代码来做。
results="100 Mbits/sec 110 Mbits/sec 90 Mbits/sec"
number=3
speed=$(echo $results | awk '{print '"\$${number}"'}')
so the speed variable will get the value 110.
所以速度变量会得到110。
Hope this helps.
希望这个有帮助。
#6
-1
No. You can pass the value of a shell variable to an awk script just like you can pass the value of a shell variable to a C program but you cannot access a shell variable in an awk script any more than you could access a shell variable in a C program. Like C, awk is not shell. See question 24 in the comp.unix.shell FAQ at cfajohnson.com/shell/cus-faq-2.html#Q24.
不。您可以将一个shell变量的值传递给一个awk脚本就像你可以将一个shell变量的值传递给一个C程序,但你不能访问shell变量在awk脚本任何超过你可以访问一个shell变量C程序。像C, awk不是shell。在unix中看到问题24。壳牌FAQ cfajohnson.com/shell/cus -常见问题- 2. - html #抓起。
One way to write your code would be:
编写代码的一种方法是:
UL="AKHIL:AKHIL_NEW,SWATHI:SWATHI_NEW"
NUSR=$(awk -F, -v ul="$UL" 'BEGIN{print gsub(FS,""); exit}')
echo "$NUSR"
echo "$UL" | awk -F, -v nusr="$NUSR" '{print $nusr}' # could have just done print $NF
but since your original starting point:
但因为你的出发点:
UL=(AKHIL:AKHIL_NEW,SWATHI:SWATHI_NEW)
was declaring UL as an array with just one entry, you might want to rethink whatever it is you're trying to do as you may have completely the wrong approach.
将UL声明为一个只有一个条目的数组,您可能想要重新考虑您想要做什么,因为您可能完全错误的方法。