-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
  • 25,269
  • 4
  • 47
  • 72
Stat.Enthus
  • 335
  • 1
  • 12

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
Jay M
  • 3,736
  • 1
  • 24
  • 33
0

Combine search and parse with sed:

echo '    Lost Workers: 0' | sed -n '/Lost/ s/.*[[:blank:]]//p'
Cole Tierney
  • 9,571
  • 1
  • 27
  • 35
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
  • 204,365
  • 48
  • 270
  • 300