Home > Enterprise >  Match a specific number in a line
Match a specific number in a line

Time:01-17

In a line like this

size: 421,939,100 bytes

I have to know only if the number is a 0. In particular, I'm interested only in how to use grep to do this task.

I tried to use a regex expression, but I got nothing satisfactory.

Many thanks!

CodePudding user response:

Could you go the direct approach?

echo size: 421,939,100 bytes | grep -q "size: 0 bytes" && echo "Zero bytes"

CodePudding user response:

A ' ' ( " ") followed by a '0' ("0") followed by zero or more '0's (0{0,}) followed by zero or 1 '.' (.{0,1}) followed by zero or more '0's (0{0,}) followed by a ' ' ( " "):

Mac_3.2.57$echo "size: 0 bytes" | grep ' 00\{0,\}\.\{0,1\}0\{0,\} '
size: 0 bytes
Mac_3.2.57$echo "size: 000 bytes" | grep ' 00\{0,\}\.\{0,1\}0\{0,\} '
size: 000 bytes
Mac_3.2.57$echo "size: 0. bytes" | grep ' 00\{0,\}\.\{0,1\}0\{0,\} '
size: 0. bytes
Mac_3.2.57$echo "size: 0.0 bytes" | grep ' 00\{0,\}\.\{0,1\}0\{0,\} '
size: 0.0 bytes
Mac_3.2.57$echo "size: 0.0000 bytes" | grep ' 00\{0,\}\.\{0,1\}0\{0,\} '
size: 0.0000 bytes
Mac_3.2.57$echo "size: 00000.0000 bytes" | grep ' 00\{0,\}\.\{0,1\}0\{0,\} '
size: 00000.0000 bytes
Mac_3.2.57$echo "size: 00000.0001 bytes" | grep ' 00\{0,\}\.\{0,1\}0\{0,\} '
Mac_3.2.57$echo "size: 2 bytes" | grep ' 00\{0,\}\.\{0,1\}0\{0,\} '
Mac_3.2.57$
  • Related