bash unix awk 列等于变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30614156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
unix awk column equal to a variable
提问by once
I want to print the lines that with two columns equal to a variable, for example, input:
我想打印两列等于变量的行,例如输入:
2607s1 NC_000067.6 95.92 49 1 1 3 50 1e-14 84.2
2607s1 NC_000067.6 97.73 44 1 0 7 50 4e-14 84.2
2607s1 NC_000067.6 97.67 43 1 0 8 50 1e-13 75.0
and variables for first and last column:
和第一列和最后一列的变量:
a="2607s1"; b="84.2"
and using awk command, output:
并使用 awk 命令,输出:
2607s1 NC_000067.6 95.92 49 1 1 3 50 1e-14 84.2
2607s1 NC_000067.6 97.73 44 1 0 7 50 4e-14 84.2
I have tried the following but not work:
我尝试了以下但不起作用:
awk -v '==$a' && '==$b' test_file
cat test_file|awk '=="($a)" && =="($b)"'
cat test_file|awk '==($a) && ==($b)'
cat test_file|awk '=="$a" && =="$b"'
Moreover, I am running it in a while loop, so the $a
and $b
keep changing Please help..
此外,我在while循环运行它,所以$a
和$b
不断变化请帮助..
回答by nu11p01n73R
You are passing the shell variables to the awk command using a wrong method. It should be like
您使用错误的方法将 shell 变量传递给 awk 命令。它应该像
awk -v a="$a" -v b="$b" '==a && == b'
What it does
它能做什么
-v a="$a"
creates an awk variablea
and assigns the value of shell variable$a
to it.-v b="$b"
Creates awk variableb
.
-v a="$a"
创建一个 awk 变量a
并将 shell 变量的值分配$a
给它。-v b="$b"
创建 awk 变量b
。
OR
或者
awk '==a && == b' a="$a" b="$b" file
When you write a statement like this
当你写这样的语句时
awk -v '==$a' && '==$b' test_file
awk doesn't know what $a
$b
is because both are shell variables.
awk 不知道是什么$a
$b
,因为两者都是 shell 变量。
And the correct method of using -v
for passing shell variables is as in demonstrated in the examples.
-v
用于传递 shell 变量的正确方法如示例中所示。
From awk manuals
来自 awk 手册
-v var=val --assign var=val Assign the value val to the variable var, before execution of the program begins. Such variable values are available to the BEGIN block of an AWK program.
-v var=val --assign var=val Assign the value val to the variable var, before execution of the program begins. Such variable values are available to the BEGIN block of an AWK program.
Test测试
$ cat file
2607s1 NC_000067.6 95.92 49 1 1 3 50 1e-14 84.2
2607s1 NC_000067.6 97.73 44 1 0 7 50 4e-14 84.2
2607s1 NC_000067.6 97.67 43 1 0 8 50 1e-13 75.0
$ a="2607s1"; b="84.2"
$ awk -v a="$a" -v b="$b" '==a && == b' file
2607s1 NC_000067.6 95.92 49 1 1 3 50 1e-14 84.2
2607s1 NC_000067.6 97.73 44 1 0 7 50 4e-14 84.2
回答by RémiH
You can also do this (this avoids using -v parameters)
您也可以这样做(这样可以避免使用 -v 参数)
awk '$1=="'$a'" && $10=="'$b'"' file
awk '$1=="'$a'" && $10=="'$b'"' file
The quote '
just before $a
will turn off string interpretation.
'
之前的引用$a
将关闭字符串解释。
The quote '
just after $a
will turn on string interpretation.
紧随'
其后的引号$a
将打开字符串解释。
$a
is in this way interpreted and replaced by its value.
$a
以这种方式被解释并被它的价值所取代。
回答by Avinash Raj
You may try the below awk command.
你可以试试下面的 awk 命令。
awk -v var1="$a" -v var2="$b" '==var1&&$NF==var2' file