5

So I less my file:

less myFile.log

Then I try to search for a value:

/70.5

I've since learned less uses regex, so . is a wildcard. I've tried to escape it with no success.

  • 11
    How did you "try to escape it with no success" without using a backslash? – Xen2050 Jan 15 at 23:38
  • You answered your own question. If I used a backslash I would have succeeded, wouldn't I have..? – notAChance Jan 18 at 13:45
  • Maybe I'll rephrase it slightly: What exactly did you type when you tried to escape the decimal with no success? Or did you only press slash to search, then typed 70.5? Maybe there's a misunderstanding of "escape"? – Xen2050 Jan 18 at 22:15
29
/70\.5

will do the trick (inside less).

  • 5
    Alternatively: /70[.]5. – jamesdlin Jan 15 at 23:10
39

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.
  • Amazing trick, thanks for this! Is it specific to less? – notAChance Jan 15 at 16:52
  • @xeon48 likely it is - at least, I don't think more supports it (although other pagers may provide something equivalent) – steeldriver Jan 15 at 16:54
  • Thanks steel, I'll play around with it when I get a chance :) – notAChance Jan 15 at 16:55
  • @xeon48 : it seems specific to less, but there are some alternatives (fgrep "something" file(s) : will look for the exact string "something" in the file(s), doing a car by car comparison and not as a regexp) – Olivier Dulac Jan 16 at 12:50
3

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

enter image description here

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).

  • Thanks for this, it will come in useful. Though the lesson learned here is brush up on my regex :) – notAChance Jan 15 at 16:48
  • 1
    [0-9]*\.[0-9]* matches on a single .. * matches on zero or more. – Stéphane Chazelas Jan 15 at 17:23
  • @StéphaneChazelas, Yes, I know, and I am busy trying to get around that without getting a complicated expression ... – sudodus Jan 15 at 17:25
  • 1
    [0-9]+(\.[0-9]*)? or even [0-9]+(\.[0-9]+)? maybe? – Stephen Kitt Jan 15 at 18:04
  • @StephenKitt, I think your first expression finds the same 'candidates' as my first string \.*[0-9]+\.* in the current version of the answer. Your second string will exclude some of dots (which may be good or bad depending on what the user wants to see). – sudodus Jan 15 at 19:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.