Linux 删除与AWK匹配的2个模式的列

Linux 删除与AWK匹配的2个模式的列,linux,shell,awk,sh,Linux,Shell,Awk,Sh,多个包含ISBN、标题、观察和其他包含观察、标题、ISBN的书籍列表,我想删除列观察,打印标题作为第一列,ISBN作为第二列,如下所示 输入: 123654宇宙学更新 35647医疗保险修订版 987456司法无礼修订 更新的修辞123456 改良会阴会阴术654321 所需输出: Cosmolgy 123654 Medecine 35647 修辞123456 会阴会阴654321 我尝试了在中找到的这个代码,并操纵了几个小时,但没有结果 awk -v OFS='\t' 'NR==1{f

多个包含ISBN、标题、观察和其他包含观察、标题、ISBN的书籍列表,我想删除列观察,打印标题作为第一列,ISBN作为第二列,如下所示

输入:

  • 123654宇宙学更新
  • 35647医疗保险修订版
  • 987456司法无礼修订
  • 更新的修辞123456
  • 改良会阴会阴术654321
所需输出:

  • Cosmolgy 123654
  • Medecine 35647
  • 修辞123456
  • 会阴会阴654321
我尝试了在中找到的这个代码,并操纵了几个小时,但没有结果

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  
awk-vofs='\t''NR==1{for(i=1;i编辑:由于OP在评论中确认输入文件没有逗号分隔,因此通过查看OP的尝试,您可以尝试以下内容吗

awk '{if($1~/^[0-9]+/){print $2,$1} else {print $2,$3}}' Input_file
解释:为上述代码添加解释

awk '                   ##Starting awk program from here.
{
  if($1~/^[0-9]+/){     ##Checking if condition if a line starts with digit then do following.
    print $2,$1         ##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 $2,$3         ##Printing 2nd and 3rd field of line.
  }
}
' Input_file            ##Mentioning Input_file name here, which awk is processing.
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($2,array," ")      ##Splitting 2nd field to array named array whose delimiter is space.
  print array[1] $1        ##Printing array 1st element and 1st field of current line.
}
' Input_file               ##Mentioning Input_file name here.
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 $2,$1     ##Printing each line 2nd field and 1st field.
}
' Input_file      ##Mentioning Input_file name here.


以下解决方案认为OP的输入文件是csv和逗号分隔的

看起来您的输入文件是逗号分隔的,您想打印第一个空格分隔的字段第二列和第一个行字段。如果是这种情况,请尝试以下操作

awk '
BEGIN{
  FS=OFS=","
}
{
  split($2,array," ")
  print array[1] $1
}
' Input_file
awk '
BEGIN{
  FS=OFS=","
}
{
  print $2,$1
}
' Input_file
解释:添加上述代码的详细解释

awk '                   ##Starting awk program from here.
{
  if($1~/^[0-9]+/){     ##Checking if condition if a line starts with digit then do following.
    print $2,$1         ##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 $2,$3         ##Printing 2nd and 3rd field of line.
  }
}
' Input_file            ##Mentioning Input_file name here, which awk is processing.
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($2,array," ")      ##Splitting 2nd field to array named array whose delimiter is space.
  print array[1] $1        ##Printing array 1st element and 1st field of current line.
}
' Input_file               ##Mentioning Input_file name here.
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 $2,$1     ##Printing each line 2nd field and 1st field.
}
' Input_file      ##Mentioning Input_file name here.


如果您的完整输入文件是逗号分隔的,那么您不仅需要打印如下字段

awk '
BEGIN{
  FS=OFS=","
}
{
  split($2,array," ")
  print array[1] $1
}
' Input_file
awk '
BEGIN{
  FS=OFS=","
}
{
  print $2,$1
}
' Input_file
解释:为上述代码添加解释

awk '                   ##Starting awk program from here.
{
  if($1~/^[0-9]+/){     ##Checking if condition if a line starts with digit then do following.
    print $2,$1         ##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 $2,$3         ##Printing 2nd and 3rd field of line.
  }
}
' Input_file            ##Mentioning Input_file name here, which awk is processing.
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($2,array," ")      ##Splitting 2nd field to array named array whose delimiter is space.
  print array[1] $1        ##Printing array 1st element and 1st field of current line.
}
' Input_file               ##Mentioning Input_file name here.
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 $2,$1     ##Printing each line 2nd field and 1st field.
}
' Input_file      ##Mentioning Input_file name here.

如果标题是“1984”、“2001:太空漫游”或“第二十二条军规”,那该怎么办?我宁愿看一下你的两个列表的来源,也不愿修改不可修改的内容。@Roadowl没有t@KarimBnAbdlaziz,既然您告诉csv文件我的答案是假设的,请将您的样本像您尝试的代码一样包装在代码标签中,以使您的样本清晰,好吗它们是逗号分隔的。你能详细解释一下你的3吗answers@KarimBnAbdlaziz是的,我也会在几分钟内给我的帖子添加解释。awk'{if($1~/^[0-9]+/){print$2,$1}else{print$2,$3}"输入,_file@KarimBnAbdlaziz,当然,现在添加了详细的解释,请仔细阅读,如果有任何疑问,请让我知道,干杯。@KarimBnAbdlaziz,所有3个解决方案解释现在都添加了,干杯。