tags:

views:

32

answers:

3

Hi,

I am trying to find some float number (like -1234.5678) in a huge text file using grep, so I thought about:

grep -n '-1234.5678'

but I get errors, do you know what is the right way using grep and why? there is anything easier?

Thanks

A: 

If you enter this in command line, try

grep -n "\-1234.5678"

to avoid interpreting 1234.5678 as a flag.

KennyTM
what do I do if the number comes from a variable? I try grep -n "\${VARIABLE}" but it does not work
Werner
ok, it was my fault, i got it, thanks
Werner
+1  A: 

Use grep -e in cases where the pattern might start with a hyphen.

 grep -n -e -1234.5678
tvanfosson
I get : grep: invalid option -- '.'
Werner
now it works, thanks a lot
Werner
The convention with GNU programs is that you can use `--` to indicate the end of options. This will work with a lot of programs, while the `-e` flag is fairly specific to `grep`.
Jefromi
A: 

you can try these

$ cat file
1 2 3 -1234.5678 4 5 1.4
ass 34.55334 aslfjas

$ awk '{for(i=1;i<=NF;i++)if($i~/^-?[0-9]+\.[0-9]+$/){print $i}}' file
-1234.5678
1.4
34.55334

$ grep -oE "\-?[0-9]+\.[0-9]+" file
-1234.5678
1.4
34.55334
ghostdog74
sorry, i mean how can i directly tell, in simple commands, how to find the number "-1234.5678". in your approach, i have to filter afterwards the results. thanks anyway
Werner