-1

I have an output that looks like this

root@machine:path# someapp report | grep Lost
    Lost Workers: 0

How can I grep only the digit at the end?

Thanks

kvantour
  • 22,845
  • 4
  • 45
  • 58
Stat.Enthus
  • 307
  • 1
  • 7

3 Answers3

0

Like this:

someapp report | grep -i lost | tr -s ' ' | cut -d' ' -f4

Run app, pipe STDOUT through tr to remove runs of spaces, cut the new string using a space and then select the 4th field

test:

echo "    Lost Workers: 0" | tr -s ' ' | cut -d' ' -f4
Jason M
  • 3,157
  • 1
  • 20
  • 27
0

Combine search and parse with sed:

echo '    Lost Workers: 0' | sed -n '/Lost/ s/.*[[:blank:]]//p'
Cole Tierney
  • 8,653
  • 1
  • 25
  • 31
0

Pipelines that look like grep | cut ... or grep | tr | cut or similar are almost always better off using awk:

$ printf 'foo\nLost Workers: 0\nbar\n' | awk '/Lost/{print $NF}'
0
William Pursell
  • 190,037
  • 45
  • 260
  • 285