首页 > 解决方案 > 在awk中提取列的列?

问题描述

在下面的示例中,我以 的输出结束3000 x 3000,它可以awk再次运行以获取宽度和高度作为变量而没有空格。

$ cat << EOF >> /tmp/test
File name       : Graf3.png
File size       : 84937 Bytes
MIME type       : image/png
Image size      : 3000 x 3000
EOF
$ cat /tmp/test | awk -F':' '/Image size/ {print $2}'
 3000 x 3000

问题

有没有办法在不awk再次调用的情况下获得宽度和高度?

标签: linuxbashawk

解决方案


假设您想要 shell 中的值而不是 awk 变量:

$ dim=( $( awk '/Image size/{print $(NF-2), $NF}' file ) )
$ declare -p dim
declare -a dim=([0]="3000" [1]="3000")

推荐阅读