Unix 比较文件中的字符串并使用shell/bash获取结果的分组依据
我有一个如下的文件:Unix 比较文件中的字符串并使用shell/bash获取结果的分组依据,unix,awk,Unix,Awk,我有一个如下的文件: h1 a 1 h2 a 1 h1 b 2 h2 b 2 h1 c 3 h2 c 3 h1 c1 3 h2 c1 3 h1 c2 3 h2 c2 3 我需要像这样的输出: 2 a 1 2 b 2 6 c 3 我尝试过bash,但不知怎么的,它并没有给我预期的结果 cat sample.log | awk '{print $2 , $3}' | sort | uniq -c 2 2 a 1 2 b 2 2 c 3 2 c1 3 2 c2 3 下面我可以得到c*的结果,
h1 a 1
h2 a 1
h1 b 2
h2 b 2
h1 c 3
h2 c 3
h1 c1 3
h2 c1 3
h1 c2 3
h2 c2 3
我需要像这样的输出:
2 a 1
2 b 2
6 c 3
我尝试过bash,但不知怎么的,它并没有给我预期的结果
cat sample.log | awk '{print $2 , $3}' | sort | uniq -c
2
2 a 1
2 b 2
2 c 3
2 c1 3
2 c2 3
下面我可以得到c*的结果,但是a和b不见了
cat sample.log | awk '$2="c" {print $2 , $3}' | sort -n | uniq -c | sort -n | tail -1
6 c 3
第一种解决方案:请尝试以下方法
awk '{sub(/[0-9]+/,"",$2);a[$2 OFS $3]++} END{for(i in a){print a[i],i}}' Input_file
说明:添加上述内容的详细说明
awk ' ##Starting awk program from here.
{
sub(/[0-9]+/,"",$2) ##Substitute digits from 2nd field with NULL.
a[$2 OFS $3]++ ##Creating array with 2nd and 3rd field and increasing its occurence.
}
END{
for(i in a){ ##Starting for loop here.
print a[i],i ##Printing array a element with index i and index i here.
}
}
' Input_file ##Mentioning Input_file name here.
第二种解决方案:如果OP需要以与输入文件相同的顺序输出,请尝试以下操作:
awk '
{
sub(/[0-9]+/,"",$2)
}
!a[$2 OFS $3]++{
count++
}
{
b[count]=$2 OFS $3
++c[$2 OFS $3]
}
END{
for(i=1;i<=count;i++){
print c[b[i]],b[i]
}
}
' Input_file
awk'
{
子(/[0-9]+/,“”,$2)
}
!a[$2/3]++{
计数++
}
{
b[计数]=3美元中的2美元
++c[$2/3]
}
结束{
对于(i=1;i第一种解决方案:请尝试以下内容
awk '{sub(/[0-9]+/,"",$2);a[$2 OFS $3]++} END{for(i in a){print a[i],i}}' Input_file
说明:添加上述内容的详细说明
awk ' ##Starting awk program from here.
{
sub(/[0-9]+/,"",$2) ##Substitute digits from 2nd field with NULL.
a[$2 OFS $3]++ ##Creating array with 2nd and 3rd field and increasing its occurence.
}
END{
for(i in a){ ##Starting for loop here.
print a[i],i ##Printing array a element with index i and index i here.
}
}
' Input_file ##Mentioning Input_file name here.
第二种解决方案:如果OP需要以与输入文件相同的顺序输出,请尝试以下操作:
awk '
{
sub(/[0-9]+/,"",$2)
}
!a[$2 OFS $3]++{
count++
}
{
b[count]=$2 OFS $3
++c[$2 OFS $3]
}
END{
for(i=1;i<=count;i++){
print c[b[i]],b[i]
}
}
' Input_file
awk'
{
子(/[0-9]+/,“”,$2)
}
!a[$2/3]++{
计数++
}
{
b[计数]=3美元中的2美元
++c[$2/3]
}
结束{
对于(i=1;i您可以使用此gnu awk
:
awk '{ ch=substr($2, 1, 1); ++freq[ch OFS $3] } END {
PROCINFO["sorted_in"] = "@ind_str_asc"; for (i in freq) print freq[i], i }' file
您可以使用此gnu awk
:
awk '{ ch=substr($2, 1, 1); ++freq[ch OFS $3] } END {
PROCINFO["sorted_in"] = "@ind_str_asc"; for (i in freq) print freq[i], i }' file
无awk
$ sed -E 's/[^ ]+ (.).* /\1 /' file | sort | uniq -c
2 a 1
2 b 2
6 c 3
无awk
$ sed -E 's/[^ ]+ (.).* /\1 /' file | sort | uniq -c
2 a 1
2 b 2
6 c 3
为什么可以使用单个awk执行3个命令?而您自己是awk
专家:)为什么可以使用单个awk执行3个命令?而您自己是awk
专家:)