This question already has answers here:
How do I use shell variables in an awk script?

(7个答案)


2年前关闭。




我有看起来像这样的脚本
#!/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.

我上面要做的是在AWK中打印出之前创建的名为“basen”的变量。
但是它以某种方式未能实现我希望的那样。

因此,例如myfile.txt包含这些行
foo bar bax
foo qux bar

通过以上bash脚本,我希望得到
myfile bax
myfile bar

什么是正确的方法?

最佳答案

您可以像这样使用它。

for i in `find $1 -name \*.jar`
do
jar tvf $i| awk -F '/' '/class/{print "'${i}'" " " $NF }' >> $classFile
done

你应该用



在AWK中使用



在Bash脚本中创建。

关于linux - 如何使AWK使用在Bash脚本中创建的变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3098713/

10-15 00:33
查看更多