Search code examples
bashshellgrepwc

Count lines with integers in them using bash and wc -l


A utility on my Linux machine outputs a log to 'stdout'. In each line, there is either a String or an Integer, it looks like the following:

[ERROR] resource busy, retrying
0989282882
[DEBUG] starting process with pid 4028
7918361566
1037491392
[DEBUG] starting process with pid 4056
2873187983
7853738301
1290312037
[DEBUG] done with init
1872989829
[DEBUG] cleaning up
8917982882

(In reality it is way longer)

I want to count the lines that have integers in them.

I already used: program | grep DEBUG | wc -l to get the debug count (same for ERROR)

But how can I count the integers? They don't have anything static like the strings, that I could grep...


Solution

  • You can use grep after specifying a regular expression to identify integers.

    For this particular case, grep -E "^[0-9]+" | wc -l should work.

    Here,

    ^ means the start of the line

    [0-9] means any number from 0 to 9

    + means one or more such digits