I learned that in awk, $2 is the 2nd column. How to specify the ith line and the element at the ith row and jth column?
我知道在awk中,$2是第二列。如何指定第i行和第i列的元素?
5 个解决方案
#1
108
To print the second line:
打印第二行:
awk 'FNR == 2 {print}'
To print the second field:
打印第二个字段:
awk '{print $2}'
To print the third field of the fifth line:
打印第五行第三个字段:
awk 'FNR == 5 {print $3}'
Edit: Here's an example with a header line and (redundant) field descriptions:
编辑:这里有一个标题行和(冗余)字段描述的示例:
awk 'BEGIN {print "Name\t\tAge"} FNR == 5 {print "Name: "$3"\tAge: "$2}'
There are better ways to align columns than "\t\t" by the way.
还有比“\t\t”更好的对齐列的方法。
#2
25
To print the columns with a specific string, you use the // search pattern. For example, if you are looking for second columns that contains abc:
要打印带有特定字符串的列,可以使用//搜索模式。例如,如果您正在寻找包含abc的第二列:
awk '$2 ~ /abc/'
... and if you want to print only a particular column:
…如果你只想打印一个特定的专栏:
awk '$2 ~ /abc/ { print $3 }'
... and for a particular line number:
…对于特定的行号:
awk '$2 ~ /abc/ && FNR == 5 { print $3 }'
#3
21
To expand on Dennis's answer, use awk
's -v
option to pass the i
and j
values:
要扩展Dennis的答案,使用awk的-v选项传递i和j值:
# print the j'th field of the i'th line
awk -v i=5 -v j=3 'FNR == i {print $j}'
#4
0
Since awk and perl are closely related...
由于awk和perl是密切相关的……
Perl equivalents of @Dennis's awk solutions:
Perl等价于@Dennis的awk解决方案:
To print the second line:perl -ne 'print if $. == 2' file
要打印第二行:perl -ne 'print if $。= = 2的文件
To print the second field:perl -lane 'print $F[1]' file
要打印第二个字段:perl -lane 'print $F[1]'文件
To print the third field of the fifth line:perl -lane 'print $F[2] if $. == 5' file
要打印第5行的第三个字段:perl -lane '打印$F[2](如果$)。= = 5的文件
Perl equivalent of @Glenn's solution:
Perl等价于@Glenn的解决方案:
Print the j'th field of the i'th line
打印第i行的第j个字段
perl -lanse 'print $F[$j-1] if $. == $i' -- -i=5 -j=3 file
perl -lanse 'print $F[$j-1] if $。== $i' -i=5 -j=3个文件
Perl equivalents of @Hai's solutions:
Perl相当于@Hai的解决方案:
if you are looking for second columns that contains abc:
如果你正在寻找包含abc的第二列:
perl -lane 'print if $F[1] =~ /abc/' foo
如果$F[1] =~ /abc/' foo,则perl -lane 'print
... and if you want to print only a particular column:
…如果你只想打印一个特定的专栏:
perl -lane 'print $F[2] if $F[1] =~ /abc/' foo
perl -lane '打印$F[2],如果$F[1] =~ /abc/' foo
... and for a particular line number:
…对于特定的行号:
perl -lane 'print $F[2] if $F[1] =~ /abc/ && $. == 5' foo
perl -lane '打印$F[2],如果$F[1] =~ /abc/ & $。= = 5 ' foo
-l
removes newlines, and adds them back in when printing-a
autosplits the input line into array @F
, using whitespace as the delimiter-n
loop over each line of the input file-e
execute the code within quotes$F[1]
is the second element of the array, since Perl starts at 0
$.
is the line number
- l删除换行,并将它们添加在印刷时——autosplit @F输入行成数组,使用空格作为分隔符- n遍历输入文件的每一行- e执行代码中引用$ F[1]是第二个元素的数组,因为Perl始于0美元。是行号
#5
0
I found this working command
我找到了这个工作命令
root@gateway:/home/sshuser# aws ec2 describe-instances --instance-ids i-2db0459d |grep 'STATE\|TAG' |awk 'FNR == 1 {print $1}'
root@gateway:/home/sshuser# aws ec2描述实例——实例id i-2db0459d |grep 'STATE\|标签' |awk 'FNR = 1 {print $1}'
STATE
状态
#1
108
To print the second line:
打印第二行:
awk 'FNR == 2 {print}'
To print the second field:
打印第二个字段:
awk '{print $2}'
To print the third field of the fifth line:
打印第五行第三个字段:
awk 'FNR == 5 {print $3}'
Edit: Here's an example with a header line and (redundant) field descriptions:
编辑:这里有一个标题行和(冗余)字段描述的示例:
awk 'BEGIN {print "Name\t\tAge"} FNR == 5 {print "Name: "$3"\tAge: "$2}'
There are better ways to align columns than "\t\t" by the way.
还有比“\t\t”更好的对齐列的方法。
#2
25
To print the columns with a specific string, you use the // search pattern. For example, if you are looking for second columns that contains abc:
要打印带有特定字符串的列,可以使用//搜索模式。例如,如果您正在寻找包含abc的第二列:
awk '$2 ~ /abc/'
... and if you want to print only a particular column:
…如果你只想打印一个特定的专栏:
awk '$2 ~ /abc/ { print $3 }'
... and for a particular line number:
…对于特定的行号:
awk '$2 ~ /abc/ && FNR == 5 { print $3 }'
#3
21
To expand on Dennis's answer, use awk
's -v
option to pass the i
and j
values:
要扩展Dennis的答案,使用awk的-v选项传递i和j值:
# print the j'th field of the i'th line
awk -v i=5 -v j=3 'FNR == i {print $j}'
#4
0
Since awk and perl are closely related...
由于awk和perl是密切相关的……
Perl equivalents of @Dennis's awk solutions:
Perl等价于@Dennis的awk解决方案:
To print the second line:perl -ne 'print if $. == 2' file
要打印第二行:perl -ne 'print if $。= = 2的文件
To print the second field:perl -lane 'print $F[1]' file
要打印第二个字段:perl -lane 'print $F[1]'文件
To print the third field of the fifth line:perl -lane 'print $F[2] if $. == 5' file
要打印第5行的第三个字段:perl -lane '打印$F[2](如果$)。= = 5的文件
Perl equivalent of @Glenn's solution:
Perl等价于@Glenn的解决方案:
Print the j'th field of the i'th line
打印第i行的第j个字段
perl -lanse 'print $F[$j-1] if $. == $i' -- -i=5 -j=3 file
perl -lanse 'print $F[$j-1] if $。== $i' -i=5 -j=3个文件
Perl equivalents of @Hai's solutions:
Perl相当于@Hai的解决方案:
if you are looking for second columns that contains abc:
如果你正在寻找包含abc的第二列:
perl -lane 'print if $F[1] =~ /abc/' foo
如果$F[1] =~ /abc/' foo,则perl -lane 'print
... and if you want to print only a particular column:
…如果你只想打印一个特定的专栏:
perl -lane 'print $F[2] if $F[1] =~ /abc/' foo
perl -lane '打印$F[2],如果$F[1] =~ /abc/' foo
... and for a particular line number:
…对于特定的行号:
perl -lane 'print $F[2] if $F[1] =~ /abc/ && $. == 5' foo
perl -lane '打印$F[2],如果$F[1] =~ /abc/ & $。= = 5 ' foo
-l
removes newlines, and adds them back in when printing-a
autosplits the input line into array @F
, using whitespace as the delimiter-n
loop over each line of the input file-e
execute the code within quotes$F[1]
is the second element of the array, since Perl starts at 0
$.
is the line number
- l删除换行,并将它们添加在印刷时——autosplit @F输入行成数组,使用空格作为分隔符- n遍历输入文件的每一行- e执行代码中引用$ F[1]是第二个元素的数组,因为Perl始于0美元。是行号
#5
0
I found this working command
我找到了这个工作命令
root@gateway:/home/sshuser# aws ec2 describe-instances --instance-ids i-2db0459d |grep 'STATE\|TAG' |awk 'FNR == 1 {print $1}'
root@gateway:/home/sshuser# aws ec2描述实例——实例id i-2db0459d |grep 'STATE\|标签' |awk 'FNR = 1 {print $1}'
STATE
状态