ros*_*ose 2 text-processing files
我有两个文件:
F1.txt
:
a:erwer|ee
b:eeeee|eee
c:ewrew|erwe
Run Code Online (Sandbox Code Playgroud)
和F2.txt
:
a.T1
a.T2
b.T3
C.T7
c.T4
Run Code Online (Sandbox Code Playgroud)
我需要通过检查 F2.txt 中的 F1.txt) 来检查a
, b
,关键字出现的次数。c
F1.txt 中的预期输出:
a:erwer|ee:total:2
b:eeeee|eeet:total:1
c:ewrew|erwe:total:2
Run Code Online (Sandbox Code Playgroud)
更新另一个文件中的o/p:
a:2
b:1
c:2
Run Code Online (Sandbox Code Playgroud)
如果您的文件不太大,您可以使用 awk :
awk '
BEGIN{FS=".";OFS=":"}
NR==FNR{a[tolower($0)]=0;next}
{
if(tolower($1) in a){
a[tolower($1)]++
}
}
END{
for(key in a){
print key, a[key]
}
}
' F1.txt F2.txt
Run Code Online (Sandbox Code Playgroud)
如果您想要区分大小写的内容,请删除该tolower
功能。
对于您编辑的问题:
awk '
BEGIN{FS="[:.]";OFS=":"}
NR==FNR{l[tolower($1)]=$0;cpt[tolower($1)]=0;next}
{
if(tolower($1) in cpt){
cpt[tolower($1)]++
}
}
END{
for(key in cpt){
print l[key],"total",cpt[key]
}
}
' F1.txt F2.txt
Run Code Online (Sandbox Code Playgroud)