首页 > 解决方案 > 在 Linux 中的文件内分组依据

问题描述

我有一个名为VPN.txt以下的文件:

VPN1 Human 1 Disconnected 
VPN1 Human 2 Disconnected
VPN1 Human 3 Is Connected
VPN2 Human 4 Connected
VPN3 Human 5 Disconnected
VPN3 Human 6 Connected 
VPN4 Human 7 Disconnected
VPN5 Human 8 Connected

然后我希望得到这样的结果:

VPN1 :
Human 1 Disconnected 
Human 2 Disconnected
Human 3 Is Connected

VPN2 : 
Human 4 Connected

VPN3 : 
Human 5 Disconnected
Human 6 Connected 

VPN4 : 
Human 7 Disconnected

VPN5 : 
Human 8 Connected

目前我从 awk 开始,我能够得到每个我当前结果的计数:

VPN1: 3
VPN2 : 1
VPN3 : 2
VPN4 : 1
VPN5 : 1

功能:

awk '{count[$1]++}END{for(j in count) print j":"count[j]}' VPN.txt

但这不是我想要做的,而不是计数我想将它们分组

标签: linuxunixawk

解决方案


请您尝试以下操作。

awk '
{
  val=$1
  $1=""
  sub(/^ +/,"")
  a[val]=(a[val]?a[val] ORS:"")$0
}
END{
  for(i in a){
    print i":" ORS a[i]
  }
}
' Input_file

说明:在此处添加对上述代码的说明。

awk '                                   ##Starting awk program from here.
{                                       ##Starting main BLOCK for this awk program from here.
  val=$1                                ##Creating val variable whose value is $1 of current line.
  $1=""                                 ##Nullifying $1 of current line here.
  sub(/^ +/,"")                         ##Substituting initial space with NULL here.
  a[val]=(a[val]?a[val] ORS:"")$0       ##Creating array a whose index is variable val and value is current line value.
}                                       ##Closing main BLOCK of this program here.
END{                                    ##Starting END BLOCK of this awk program here.
  for(i in a){                          ##Starting a for loop to traverse through array a here.
    print i":" ORS a[i]                 ##Printing variable i colon ORS and value of array a with variable i here.
  }                                     ##Closing for loop previous BLOCK here.
}                                       ##Closing BLOCK for END section of this awk program here.
'  Input_file                           ##Mentioning Input_file name here


第二种解决方案:如果您想以与 Input_file 的第一个字段发生的顺序相同的顺序输出,那么您可以尝试以下操作。

awk '
{
  val=$1
  $1=""
  sub(/^ +/,"")
}
!c[val]++{
  d[++count]=val
}
{
  a[val]=(a[val]?a[val] ORS:"")$0
}
END{
  for(i=1;i<=count;i++){
    print d[i]":" ORS a[d[i]]
  }
}
'   Input_file

推荐阅读