Home > Blockchain >  How can I calculate the number of occurrences that is followed by a specific value? (add if statemen
How can I calculate the number of occurrences that is followed by a specific value? (add if statemen

Time:10-21


How can I calculate the number of occurrences that are ONLY followed by a specific value that is after E*? e.g:'EXXXX' ?

file.txt:

E2dd,Rv0761,Rv1408  
2s32,Rv0761,Rv1862,Rv3086  
6r87,Rv0761
Rv2fd90c,Rv1408
Esf62,Rv0761
Evsf62,Rv3086

i tried input:

awk -F, '{map[$2]  } END { for (key in map) { print key, map[key] } }' file.txt

and add:

if [[ $line2 == `E*` ]];then

but not working, have syntax error

Expected Output:

total no of occurrences: 
Rv0761: 2
Rv3086:1

Now i can only count all number of occurrences of the second value

CodePudding user response:

if [[ $line2 == `E*` ]];then

This definitely is not legal GNU AWK if statement, consult If Statement to find what is allowed, though it is not required in this case as you might as follows, let file.txt content be

E2dd,Rv0761,Rv1408  
2s32,Rv0761,Rv1862,Rv3086  
6r87,Rv0761
Rv2fd90c,Rv1408
Esf62,Rv0761
Evsf62,Rv3086

then

awk 'BEGIN{FS=","}($1~/^E/){map[$2]  } END { for (key in map) { print key, map[key] } }' file.txt

gives output

Rv3086 1
Rv0761 2

Explanation: actions (enclosed in {...}) could be preceeded by pattern, which does restrict their execution to lines which does match pattern (in other words: condition does hold) in above example pattern is $1~/^E/ which means 1st column does starts with E.

(tested in gawk 4.2.1)

CodePudding user response:

rq (https://github.com/fuyuncat/rquery/releases) can do this.
It will find out all rows with E* started, then convert the following word to rows and group & count them.

[ rquery]$ cat samples/efile.txt
E2dd,Rv0761,Rv1408
2s32,Rv0761,Rv1862,Rv3086
6r87,Rv0761
Rv2fd90c,Rv1408
Esf62,Rv0761
Evsf62,Rv3086
[ rquery]$ ./rq -q "p d/,/ | s coltorow(foreach(2,%,trim($))) | f @1 like 'E*'" samples/efile.txt | ./rq -q "s @raw,count(1) | g @raw"
Rv0761  2
Rv1408  1
Rv3086  1
  • Related