开发者

Find float in a text file

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 an开发者_JAVA百科d why? there is anything easier?

Thanks


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

 grep -n -e -1234.5678


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


If you enter this in command line, try

grep -n "\-1234.5678"

to avoid interpreting 1234.5678 as a flag.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜