2

I have several sentences in a file in this format:

"my string -2.22 rest of string"
"my string -1.00 rest of string"
...

The numbers are all negative.

I want to grep for these sentences.

Right now I'm doing: grep "my string -d\+ rest of string" but it doesn't seem to work.

gruuuvy
  • 2,118
  • 4
  • 26
  • 41

1 Answers1

2

Using grep you can do:

grep 'my string [0-9.-]\+ rest of string' file

Or using extended regex:

grep -E 'my string [0-9.-]+ rest of string' file

[0-9.-]+ matches 1 or more of any digit or DOT or hyphen

Note that this will allow multiple - or . in the number.

If you want to do precise matching then use:

grep -E 'my string [+-]?[0-9]*\.[0-9]+ rest of string' file
anubhava
  • 664,788
  • 59
  • 469
  • 547