How do I search a file using "less" for a value with a decimal point?
You can turn off regex mode by hitting Ctrl+R before typing the pattern:
^R Don't interpret regular expression metacharacters; that is, do a simple textual comparison.
/70\.5
will do the trick (inside less
).
Two search expressions for numbers in less
/\.*[0-9]+\.* # for numbers
/[0-9]*\.[0-9]+ # for numbers with a decimal part
Regex to search for numbers (with or without a decimal)
This regex works in less
but also in other cases where the same regex syntax is used.
\.*[0-9]+\.*
You start the search engine with /
, so if you want to find decimal numbers, but avoid text with dots (like file.txt) or periods between sentences, I think the following string is rather good,
/\.*[0-9]+\.*
Test file
There are several ways to use a dot. Here are some examples:
- 'Period' finishing a sentence
- Short for current directory or 'source' command in linux shells
- Separator between the name and extension of a file
- Separator in between the integer part and decimal part of a number
- Separator in special numerical or litteral strings (for example IP adress)
The following regex expression is rather simple and can identify
- numbers
- numerial strings
\.*[0-9]+\.*
.bashrc
hello-0
170.5
text.txt
170
170.
.551
asdf 170.5 qwerty
192.168.1.1
file.000
file.001
Regex to search for numbers with a decimal part
This regex works in less
but also in other cases where the same regex syntax is used.
[0-9]*\.[0-9]+
The corresponding search command is
/[0-9]*\.[0-9]+
It will also find numerical strings (for example IP address), in general digits after a dot (including digits before the dot, if any).