Home > Software engineering >  Counting all the 5 from a specific range in Bash
Counting all the 5 from a specific range in Bash

Time:11-02

I want to count how many times the digit "5" appears from the range 1 to 4321. For example, the number 5 appears 1 or the number 555, 5 would appear 3 times etc.

Here is my code so far, however, the results are 0, and they are supposed to be 1262.

#!/bin/bash
typeset -i count5=0
for n in {1..4321}; do
        echo ${n}
done | \
     while read -n1 digit ; do
        if [ `echo "${digit}" | grep 5` ] ; then
                count5=count5 1
        fi
     done | echo "${count5}"

P.s. I am looking to fix my code so it can print the right output. I do not want a completely different solution or a shortcut.

CodePudding user response:

try rq (https://github.com/fuyuncat/rquery/releases)
seq generates the numbers in the range, countstr counts how many 5 in the numbers.

[ rquery]$ seq 4321 | ./rq -q "s @raw, countstr(@raw,'5')"
...
3650    1
3651    1
3652    1
3653    1
3654    1
3655    2
3656    1
3657    1
3658    1
3659    1
3660    0
3661    0
...

CodePudding user response:

One primary issue:

  • each time results are sent to a pipe said pipe starts a new subshell; in bash any variables set in the subshell are 'lost' when the subshell exits; net result is even if you're correctly incrementing count5 within a subshell you'll still end up with 0 (the starting value) when you exit from the subshell

Making minimal changes to OP's current code:

 while read -n1 digit ; do
    if [ `echo "${digit}" | grep 5` ]; then
            count5=count5 1
    fi
 done < <(for n in {1..4321}; do echo ${n}; done)

 echo "${count5}"

NOTE: there are a couple performance related issues with this method of coding but since OP has explicitly asked to a) 'fix' the current code and b) not provide any shortcuts ... we'll leave the performance fixes for another day ...

  • Related