下面有一个名为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
但这不是我想做的,我不想数,我想把他们分组
发布于 2019-12-18 10:26:15
你能试一下吗。
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
发布于 2019-12-18 11:03:05
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
prev!=$1
):ORS
= output字段分隔符,后面跟着空格字符:
和newline)
如果separator值没有更改:
OFS
(空格字符)作为分隔符
更新
如果它包含一个可选的空格字符(是的,它是field1和field2,但是让我们称之为“第一个字段”),这个版本将与“第一个字段”相匹配。
这假设第一个字段仅包含
根据需要更改正则表达式。
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
https://stackoverflow.com/questions/59389873
复制相似问题