我有以下格式的文件
/////
name 1
start_occurrence:
occurrence 1
occurrence 2
///
name 2
start_occurance:
occurrence 1
occurrence 2
///
name 3
start_occurrence:
occurrence 1
occurrence 2
occurrence 3
我需要的是计算每个名称的出现次数并将其保存在CSV文件中。我可以使用任何shell命令组合吗?是的我可以通过编程方式进行,但是以管道方式查找一堆shell命令。
" names
"可以是任何东西。名称没有图案。唯一的问题是///
之后的行是名称。 Occurrence
也没有任何数字,以occurrence
或occurrence
开头的任意行都是您感兴趣的主题。
答案 0 :(得分:1)
awk 'c=="THISISNAME"{b=$0;c="";}$1=="///"{c="THISISNAME"}$0~/\<occurrence\>/{a[b]+=1;}END{for (i in a){print i" "a[i]}}' YOUR_FILE_HERE
解释
if match the name start condition ($1=="///"), mark the c to THISISNAME.
if this is the name line (c=="THISISNAME"), mark the name line with b, and mark c as name part ended(c="").
if match the occurrence condition ($0~/\<occurrence\>/), make a[b] += 1.
use a map a to remark the occurrence time of each name.
awk使用ERE,$ 0~ / EREs /表示$ 0与正则表达式匹配。 &#39; \&lt;&#39;和&#39;&gt;&#39;意味着&#39; \ b&#39;在PREs