linux - 在 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
但这不是我想要做的,而不是计数我想将它们分组
解决方案
请您尝试以下操作。
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
推荐阅读
- azure - 在 Azure ACI(容器)或 AKS(Kubernetes)上部署 ML 模型
- reactjs - 当使用离子反应从选择框中选择其他选项时显示输入字段
- python - 在 matplotlib python 中没有得到预期的结果
- linux - 如何使用内核映像启动 qemu-system-aarch64?
- javascript - 创建香蕉对象
- ruby-on-rails - 在不创建模型和控制器的情况下路由到不同的视图集
- flutter - Flutter:有什么方法可以在一个文本字段中更改提示文本的颜色吗?
- python - 实现基于流派分组购买的python代码
- keycloak - 我可以通过 Keycloak 在有效负载令牌中添加一些信息吗?
- html - 是否可以使用css将两个图像合并为一个形状?