Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count b or B in even lines

Tags:

awk

I need count the number of times in the even lines of the file.txt the letter 'b' or 'B' appears, e.g. for the file.txt like:

everyB or gbnBra
uitiakB and kanapB bodddB
Kanbalis astroBominus

I got the first part but I need to count these b or B letters and I do not know how to count them together

awk '!(NR%2)' file.txt
like image 431
Olek Avatar asked May 23 '20 00:05

Olek


People also ask

How do you count an even number?

If N is even then the count of both odd and even numbers will be N/2. If N is odd, If L or R is odd, then the count of the odd numbers will be N/2 + 1, and even numbers = N – countofOdd. Else, the count of odd numbers will be N/2 and even numbers = N – countofOdd.

What is b percentage?

B (70-74%): Work shows some limitations in coverage and some minor errors in fact or credible interpretation.

What is even number example?

Any number that can be exactly divided by 2 is called as an even number. Even numbers always end up with the last digit as 0, 2, 4, 6 or 8. Some examples of even numbers are 2, 4, 6, 8, 10, 12, 14, 16. These are even numbers as these numbers can easily be divided by 2.


Video Answer


3 Answers

$ awk '!(NR%2){print gsub(/[bB]/,"")}' file
4
like image 74
Ed Morton Avatar answered Oct 20 '22 14:10

Ed Morton


Could you please try following, one more approach with awk written on mobile will try it in few mins should work but.

awk -F'[bB]' 'NR%2 == 0{print (NF ? NF - 1 : 0)}' Input_file

Thanks to @Ed sir for solving zero matches found line problem in comments.

like image 35
RavinderSingh13 Avatar answered Oct 20 '22 15:10

RavinderSingh13


In a single awk:

awk '!(NR%2){gsub(/[^Bb]/,"");print length}' file.txt

gsub(/[^Bb]/,"") deletes every character in the line the line except for B and b.

print length prints the length of the resulting string.

like image 3
Quasímodo Avatar answered Oct 20 '22 14:10

Quasímodo