awk linux shell变量调用shell变量

发布于 2025-02-11 19:58:05 字数 716 浏览 1 评论 0原文

这可能是一个简单的/什么,这个问题是:在这里,我加载4个变量$ 1 $ 2 $ 3和$ 4 说

file1   
1 2 3  
1 4 5  
1 6 7  
myloop () {
 echo col=$1 $2 $3 $4
 awk -v c1="$1" -v c2="$2" -F, 'BEGIN {
        FS=" ";
 }
 {
        xy+=($c1*$c2);
 }
 END {
        print "Product" $3 "and " $4  #this is where $3 (='h2') and $4 (='02') don't print
        print "prod= " xy;
 }' file1 > res.txt
}
myloop 2 3 h2 o2

res.txt中的预期输出,

product h2 and 02  
prod = 68  

因此,正如我在代码中注释的行中所说的那样,我无法阅读3美元和4美元。
我尝试通过$ 3和$ 4的
awk -v c1 =“ $ 1” -v c2 =“ $ 2” -vl1 =“ $ 3” -v l2 =“ $ 4” ...,然后在打印语句中调用$ l1和$ l2但这不起作用。
任何提示都将不胜感激,因为我不是脚本的专家(您可以告诉)
提前致谢

this is probably an easy/what-the-hell-is-that question: here a blueprint of the script where I load 4 variables $1 $2 $3 and $4 in a function 'myloop'
say

file1   
1 2 3  
1 4 5  
1 6 7  
myloop () {
 echo col=$1 $2 $3 $4
 awk -v c1="$1" -v c2="$2" -F, 'BEGIN {
        FS=" ";
 }
 {
        xy+=($c1*$c2);
 }
 END {
        print "Product" $3 "and " $4  #this is where $3 (='h2') and $4 (='02') don't print
        print "prod= " xy;
 }' file1 > res.txt
}
myloop 2 3 h2 o2

expected output in res.txt

product h2 and 02  
prod = 68  

so as I said in the commented line in the code, I can't get to read $3 and $4.
I try passing $3 and $4 with
awk -v c1="$1" -v c2="$2" -vl1="$3" -v l2="$4" ... and then call $l1 and $l2 in the print statement but that does not work.
any hint would be appreciated as I'm no expert (as you can tell) in scripting
Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情域 2025-02-18 19:58:06

您要寻找的称为字符串插值,可以像这样执行:

print "Product  ${3} and  ${4}"

What you are looking for is called string interpolation and can be performed like so:

print "Product  ${3} and  ${4}"
网白 2025-02-18 19:58:05
$ cat tst.sh
#!/usr/bin/env bash

myloop() {
    echo "col=$*"
    awk -v c1="$1" -v c2="$2" -v l1="$3" -v l2="$4" '
        {
            xy += $c1 * $c2
        }
        END {
            print "Product", l1, "and", l2
            print "prod =", xy+0
        }
    ' file1
}

myloop 2 3 h2 o2

$ ./tst.sh
col=2 3 h2 o2
Product h2 and o2
prod = 68

l1l2是变量名的错误选择,因为l在许多字体中看起来太像1因此,使您的代码混淆。

顺便说一句,在您的问题中,此:

awk -F,

将fs从空白的默认值更改为逗号,然后将其更改为:

BEGIN {
        FS=" ";
 }

将其设置回其必须开始的默认空白值。只是不要那样做。

$ cat tst.sh
#!/usr/bin/env bash

myloop() {
    echo "col=$*"
    awk -v c1="$1" -v c2="$2" -v l1="$3" -v l2="$4" '
        {
            xy += $c1 * $c2
        }
        END {
            print "Product", l1, "and", l2
            print "prod =", xy+0
        }
    ' file1
}

myloop 2 3 h2 o2

$ ./tst.sh
col=2 3 h2 o2
Product h2 and o2
prod = 68

l1 and l2 are bad choices for variable names though since l looks far too much like 1 in many fonts and so obfuscates your code.

By the way, in your code in your question, this:

awk -F,

is changing FS from it's default value of a blank to a comma and then this:

BEGIN {
        FS=" ";
 }

is setting it back to the default blank value it had to begin with. Just don't do that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文