1

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...

finnmglas
  • 1,626
  • 4
  • 22
  • 37

4 Answers4

3

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

1

You do not need wc for this, grep has -c

program |  grep -c -E -x '[[:digit:]]+'
  • -c print only a count of selected lines
  • -E extended regexp (you can omit it and use the pattern [0-9]\+)
  • -x match the entire line not just part of it

See POSIX grep manual

Alternatively, since you mentioned also [DEBUG], you can use awk for a single pass (especialy if your program is not idempotent)

program | awk  '/^[0-9]+$/ { numbers++ } /^\[DEBUG\]/ {debugs++} END { print numbers,debugs }'

See POSIX awk manual

Sorin
  • 5,201
  • 2
  • 18
  • 45
0

I would do this program | grep -E "^[0-9]+" | wc -l. This will match lines which start with 1 or more digits. Seems like enough for your use case.

Eraklon
  • 4,206
  • 2
  • 13
  • 29
0
program | grep '^[0-9].*$' | wc -l
Buddhi
  • 416
  • 4
  • 14