首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Linux中在文件中进行分组

在Linux中在文件中进行分组
EN

Stack Overflow用户
提问于 2019-12-18 10:21:36
回答 2查看 66关注 0票数 1

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

代码语言:javascript
运行
复制
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

那么我希望得到这样的结果:

代码语言:javascript
运行
复制
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开始,我得到了我现任领事的每一个数字:

代码语言:javascript
运行
复制
VPN1: 3
VPN2 : 1
VPN3 : 2
VPN4 : 1
VPN5 : 1

职能:

代码语言:javascript
运行
复制
awk '{count[$1]++}END{for(j in count) print j":"count[j]}' VPN.txt

但这不是我想做的,我不想数,我想把他们分组

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-12-18 10:26:15

你能试一下吗。

代码语言:javascript
运行
复制
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

解释:在这里添加对上述代码的解释。

代码语言:javascript
运行
复制
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的第一个字段的顺序相同,则可以尝试如下。

代码语言:javascript
运行
复制
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
票数 1
EN

Stack Overflow用户

发布于 2019-12-18 11:03:05

代码语言:javascript
运行
复制
awk '
prev!=$1 { prev=$1; printf "%s%s :%s", NR==1 ? "" : ORS, $1, ORS }
prev==$1 { for (i=2;i<=NF; i++){ printf "%s%s", $i, i==NF ? ORS : OFS} }
' file

如果prev

  • print的值发生了变化(prev!=$1):
  • 将其新值保存在变量中,如果它不是第一行(ORS= output字段分隔符,后面跟着空格字符:和newline

)

如果separator值没有更改:

  • 打印除第一个字段外的所有字段,如果该字段不是最后一个字段,则使用输出字段分隔符OFS (空格字符)作为分隔符
  • ,如果它是最后一个字段,则使用换行符作为

更新

如果它包含一个可选的空格字符(是的,它是field1和field2,但是让我们称之为“第一个字段”),这个版本将与“第一个字段”相匹配。

这假设第一个字段仅包含

  • 大写字母和数字
  • 或大写字母,空格字符和数字

根据需要更改正则表达式。

代码语言:javascript
运行
复制
awk '
BEGIN { regex="^([A-Z0-9]+|[A-Z]+ [0-9]+)" }     # set regex to match "first field"
{ match($0, regex, a); f1=a[0] }                 # set "first field" as f1
prev!=f1 {
  prev=f1                                        # use f1 instead of $1 as previous field
  printf "%s%s :%s", NR==1 ? "" : ORS, prev, ORS # copy & paste from script above
}
prev==f1 { 
  sub(regex" ", "") # remove "first field" and the following space character from $0
  print             # print the line (now without "first field")
}' file
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59389873

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档