gpt4 book ai didi

linux - 如何让 AWK 使用 Bash 脚本中创建的变量

转载 作者:IT王子 更新时间:2023-10-29 00:23:10 27 4
gpt4 key购买 nike

<分区>

我有这样的脚本

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

正确的做法是什么?

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com