Grep float/integers but skip some integers

Hi,
I am working in bash in Mac OSX, I have following 'input.txt' file:

<INFO> HypoTestTool: >>> Done running HypoTestInverter on the workspace combined
<INFO> HypoTestTool: The computed upper limit is: 11 +/- 1.02651
<INFO> HypoTestTool: expected limit (median) 11
<INFO> HypoTestTool: expected limit (-1 sig) 9.84211
<INFO> HypoTestTool: expected limit (+1 sig) 11
<INFO> HypoTestTool: expected limit (-2 sig) 0.202636
<INFO> HypoTestTool: expected limit (+2 sig) 11

I want to grep all the integers and floats except -1 +1 -2 +2 before "sig".

I tried this

cat input.txt | grep -oE '\-?[0-9]+|\-?[0-9]+\.[0-9]+' 

but i also get -1 +1 -2 +2 ,

I don't want to restrict the length of integers like this:

cat input.txt | grep -oE '\-?[0-9][0-9]|\-?[0-9]+\.[0-9]+' 

Can you please help in it ?

This will only get the last integer on a line.

awk '$NF ~ /[0-9]/ { print $NF }' inputfile
grep -o '[^+-][0-9]\+[.]*[0-9]*' input.txt
1 Like

THanks Corona688 and especially to Yoda,
I skip number before "+/-" if i use Corona's solution but Yoda's solution is suitable for me :slight_smile:

---------- Post updated at 04:26 PM ---------- Previous update was at 04:03 PM ----------

How would you take care if any number is negative ? I mean if a number has minus sign then grep should print - sign with that number.

$ cat input.txt
<INFO> HypoTestTool: >>> Done running HypoTestInverter on the workspace combined
<INFO> HypoTestTool: The computed upper limit is: 11 +/- 1.02651
<INFO> HypoTestTool: expected limit (median) 11
<INFO> HypoTestTool: expected limit (-1 sig) -9.84211
<INFO> HypoTestTool: expected limit (+1 sig) 11
<INFO> HypoTestTool: expected limit (-2 sig) 0.202636
<INFO> HypoTestTool: expected limit (+2 sig) +11
$ grep -o '[+-]*[0-9]\+[.]*[0-9]* *$' input.txt
1.02651
11
-9.84211
11
0.202636
+11

Thanks hanson,

But

$ grep -o '[+-]*[0-9]\+[.]*[0-9]* *$' input.txt

is skipping first number '11' before "+/-" , in output there should be 7 numbers.

Here's a possible way:

$ cat input.txt
<INFO> HypoTestTool: >>> Done running HypoTestInverter on the workspace combined
<INFO> HypoTestTool: The computed upper limit is: 11 +/- 1.02651
<INFO> HypoTestTool: expected limit (median) 11
<INFO> HypoTestTool: expected limit (-1 sig) -9.84211
<INFO> HypoTestTool: expected limit (+1 sig) 11
<INFO> HypoTestTool: expected limit (-2 sig) 0.202636
<INFO> HypoTestTool: expected limit (+2 sig) +11
$ grep -o '[+-]*[0-9]\+[.]*[0-9]*' input.txt | grep -v "[+-][12]\>"
11
1.02651
11
-9.84211
11
0.202636
+11
1 Like

Many thanks hanson!

Try this simple sed regex:

$ sed 's:^\([^-+0-9]*\|.*)\)::g' file

11 +/- 1.02651
11
 -9.84211
 11
 0.202636
 +11

Sorry but this command doesn't do anything for me.

With your file from post#1:

$ sed 's:^\([^-+0-9]*\|.*)\)::g' file


11 +/- 1.02651
11
 9.84211
 11
 0.202636
 11

Yes, still i am using the same input file but i don't see any effect of this sed command. I don't know why we see different results.

OK, mayhap your sed doesn't like the ORed regex. Try:

sed 's:^[^-+0-9]*::g; s:^.*)::' file
11 +/- 1.02651
11
 9.84211
 11
 0.202636
 11
1 Like

Yes, you are right. It works fine. Thanks