This question already has an answer here:
这个问题在这里已有答案:
- How do I use shell variables in an awk script? 7 answers
- 如何在awk脚本中使用shell变量? 7个答案
I have script that looks like this
我的脚本看起来像这样
#!/bin/bash
#exampel inputfile is "myfile.txt"
inputfile=$1
basen=`basename $inputfile .txt` # create basename
cat $inputfile |
awk '{print $basen "\t" $3} # this doesn't print "myfile" but the whole content of it.
What I want to do above is to print out in AWK the variable called 'basen' created before. But somehow it failed to do what I hoped it will.
我上面要做的是在AWK中打印之前创建的名为“basen”的变量。但不知怎的,它没能按照我的希望做到。
So for example myfile.txt
contain these lines
例如,myfile.txt包含这些行
foo bar bax
foo qux bar
With the above bash script I hope to get
使用上面的bash脚本我希望得到
myfile bax
myfile bar
What's the right way to do it?
什么是正确的方法呢?
6 个解决方案
#1
12
You can use it like this.
你可以像这样使用它。
for i in `find $1 -name \*.jar`
do
jar tvf $i| awk -F '/' '/class/{print "'${i}'" " " $NF }' >> $classFile
done
You should use
你应该用
"'${i}'"
“ '$ {I}'”
in AWK to use the
在AWK中使用
$i
$ I
created in Bash Script.
在Bash脚本中创建。
#2
29
The -v
flag is for setting variables from the command line. Try something like this:
-v标志用于从命令行设置变量。尝试这样的事情:
awk -v "BASEN=$basen" '{print BASEN "\t" $3}'
#3
4
you can just do everything in awk
你可以在awk中做所有事情
awk '{gsub(".txt","",ARGV[1]);print ARGV[1] "\t" $3}' inputfile.txt
#4
4
Assuming you run awk as the sub process of the shell you declared the vars
Within the shell
假设你运行awk作为shell的子进程,你在shell中声明了vars
export MY_VARS="whatever"; #// IT NEEDS to be exported, to allow the sub process awk read access.
echo ""| awk '{
print "Reading values from within awk : "ENVIRON["MY_VARS"];
}'
Result:
结果:
Reading values from within awk : whatever
从awk中读取值:无论如何
notice the importance of export. With out it, the vars from the shell is considered local and does not get passed to the co-processes.
注意出口的重要性。在外壳中,来自shell的变量被认为是本地的,并且不会传递给协同进程。
#5
2
The reason is that bash variables (environment variables) are not expanded within single-quoted strings. Try replacing
原因是bash变量(环境变量)不会在单引号字符串中展开。尝试更换
'{print $basen "\t" $3}'
with
同
"{print \"$basen\" \"\t\" \$3}"
#6
1
The easiest way is to make an awk variable. awk -v awkvar=$bashvar 'awkscript'
.
最简单的方法是创建一个awk变量。 awk -v awkvar = $ bashvar'awkscript'。
#1
12
You can use it like this.
你可以像这样使用它。
for i in `find $1 -name \*.jar`
do
jar tvf $i| awk -F '/' '/class/{print "'${i}'" " " $NF }' >> $classFile
done
You should use
你应该用
"'${i}'"
“ '$ {I}'”
in AWK to use the
在AWK中使用
$i
$ I
created in Bash Script.
在Bash脚本中创建。
#2
29
The -v
flag is for setting variables from the command line. Try something like this:
-v标志用于从命令行设置变量。尝试这样的事情:
awk -v "BASEN=$basen" '{print BASEN "\t" $3}'
#3
4
you can just do everything in awk
你可以在awk中做所有事情
awk '{gsub(".txt","",ARGV[1]);print ARGV[1] "\t" $3}' inputfile.txt
#4
4
Assuming you run awk as the sub process of the shell you declared the vars
Within the shell
假设你运行awk作为shell的子进程,你在shell中声明了vars
export MY_VARS="whatever"; #// IT NEEDS to be exported, to allow the sub process awk read access.
echo ""| awk '{
print "Reading values from within awk : "ENVIRON["MY_VARS"];
}'
Result:
结果:
Reading values from within awk : whatever
从awk中读取值:无论如何
notice the importance of export. With out it, the vars from the shell is considered local and does not get passed to the co-processes.
注意出口的重要性。在外壳中,来自shell的变量被认为是本地的,并且不会传递给协同进程。
#5
2
The reason is that bash variables (environment variables) are not expanded within single-quoted strings. Try replacing
原因是bash变量(环境变量)不会在单引号字符串中展开。尝试更换
'{print $basen "\t" $3}'
with
同
"{print \"$basen\" \"\t\" \$3}"
#6
1
The easiest way is to make an awk variable. awk -v awkvar=$bashvar 'awkscript'
.
最简单的方法是创建一个awk变量。 awk -v awkvar = $ bashvar'awkscript'。