使用 AWK 删除匹配 2 个模式的列

Remove column matching 2 patterns with AWK

带有 ISBN、TITLE、OBSERVATION 和其他带有 OBSERVATION、TITLE、ISBN 的多个书籍列表,我想删除 OBSERVATION 列,将 TITLE 打印为第一列,将 ISBN 打印为第二列,如下所示。

输入:

所需输出:

我尝试了在 SE 中找到的这段代码并操作了几个小时都没有结果

awk -v OFS='\t' 'NR==1{for (i=1;i<=NF;i++)if ($i=="Updated"){n=i-1;m=NF-(i==NF)}} {for(i=1;i<=NF;i+=1+(i==n))printf "%s%s",$i,i==m?ORS:OFS}' file  

编辑: 由于 OP 在评论中确认 Input_file 不是逗号分隔然后通过查看 OP 的尝试,你能请尝试关注

awk '{if(~/^[0-9]+/){print ,} else {print ,}}' Input_file

说明:为上述代码添加说明。

awk '                   ##Starting awk program from here.
{
  if(~/^[0-9]+/){     ##Checking if condition if a line starts with digit then do following.
    print ,         ##Printing 2nd field and then 1st field of line here.
  }
  else{                 ##Using else in case that above if condition is NOT TRUE then do following.
    print ,         ##Printing 2nd and 3rd field of line.
  }
}
' Input_file            ##Mentioning Input_file name here, which awk is processing.


以下解决方案认为 OP 的 Input_file 是 csv 和逗号分隔的。

看起来你的 Input_file 是逗号分隔的,你想打印 space 的第一个字段然后分隔第二列和第一个字段。如果是这种情况,请您尝试以下。

awk '
BEGIN{
  FS=OFS=","
}
{
  split(,array," ")
  print array[1] 
}
' Input_file

说明:为以上代码添加详细说明。

awk '                      ##Starting awk program from here.
BEGIN{                     ##Starting BEGIN section from here.
  FS=OFS=","               ##Setting comma for FS and OFS here for all lines of Input_file.
}
{                          ##Starting main BLOCK from here for this program.
  split(,array," ")      ##Splitting 2nd field to array named array whose delimiter is space.
  print array[1]         ##Printing array 1st element and 1st field of current line.
}
' Input_file               ##Mentioning Input_file name here.


如果您的完整 Input_file 是逗号分隔的,那么您不仅需要打印如下字段。

awk '
BEGIN{
  FS=OFS=","
}
{
  print ,
}
' Input_file

说明:为上述代码添加说明。

awk '             ##Starting awk program from here.
BEGIN{            ##Starting BEGIN section from here.
  FS=OFS=","      ##Setting comma for FS and OFS here for all lines of Input_file.
}
{
  print ,     ##Printing each line 2nd field and 1st field.
}
' Input_file      ##Mentioning Input_file name here.