如何在bash中AWK多个列

如何在bash中AWK多个列,awk,Awk,我在下表中有多个列我只想打印第一列和第二列 以下命令生成下表: kubectl get ing -n my-namespace firstcolumn secondcolumn third columns 1 test value 3 test2 value 2 test3

我在下表中有多个列我只想打印第一列和第二列

以下命令生成下表:

kubectl get ing -n my-namespace 


firstcolumn          secondcolumn        third columns
1                    test                value
3                    test2               value
2                    test3               value
5                    test4               value
6                    test                value
预期产出应为:

firstcolumn          secondcolumn        
1                    test                
3                    test2               
2                    test3               
5                    test4               
6                    test                
下面的命令仅在第一列中
awk

kubectl get ing -n my-namespace | awk '{ printf "%10s\n", $2 }'

如何
awk
第一列和第二列?

请尝试以下内容。这将在第一个和第二个字段之间打印相同的空格

awk '{match($0,/ +/);print $1,substr($0,RSTART,RLENGTH),$2}' Input_file
解释:添加上述代码的详细解释

awk '                                        ##Starting awk program from here.
{
  match($0,/ +/)                             ##Usingawk match function which will match very first ALL spaces in current line.
  print $1,substr($0,RSTART,RLENGTH),$2      ##Now printing $1 then substring starting from RSTART to RLENGTH(which will have spaces in it) then 2nd field.
}
' Input_file                                 ##Mentioning Input_file name here.


或者,如果您不担心间距(我认为您担心的是spacig),那么您可以简单地执行以下操作:

awk '{print $1,$2}'  Input_file

你能试试下面的吗。这将在第一个和第二个字段之间打印相同的空格

awk '{match($0,/ +/);print $1,substr($0,RSTART,RLENGTH),$2}' Input_file
解释:添加上述代码的详细解释

awk '                                        ##Starting awk program from here.
{
  match($0,/ +/)                             ##Usingawk match function which will match very first ALL spaces in current line.
  print $1,substr($0,RSTART,RLENGTH),$2      ##Now printing $1 then substring starting from RSTART to RLENGTH(which will have spaces in it) then 2nd field.
}
' Input_file                                 ##Mentioning Input_file name here.


或者,如果您不担心间距(我认为您担心的是spacig),那么您可以简单地执行以下操作:

awk '{print $1,$2}'  Input_file

那么这个值可以包含空格吗?例如,我看到
第三列
带有空格,它是否也会出现在其他列上?
awk'{printf“%10s\n”,$2}'
不打印第一列,而是打印第二列(注意
$2
$1
相反)。既然
$2
表示
2
nd列,那么现在如何打印
1
st列也很明显了吗?如果不是-列之间的空白是单个制表符还是多个空白字符或其他什么?因此该值可以包含空格?例如,我看到
第三列
带有空格,它是否也会出现在其他列上?
awk'{printf“%10s\n”,$2}'
不打印第一列,而是打印第二列(注意
$2
$1
相反)。既然
$2
表示
2
nd列,那么现在如何打印
1
st列也很明显了吗?如果不是-列之间的空白是单个制表符还是多个空白字符或其他什么?