I'd like to view a text file on the terminal and highlight its numbers. Something like I get when I open it with gedit. I haven't been able to do this with less, cat, vim or other viewers/editors.

P.S. My question is straightforward, but I haven't been able to find the correct keywords for a meaningful search.

DIY solution

Solution A

Match every number and highlights it, print every line:

egrep --colour=always '[-+]?[0-9]*[\.e]?[0-9]|$'

I guess I'll simply alias this. I'm still wondering if there's a default option.

Edit: adding support for exponential e, tnx to @αғsнιη.

Solution B

Alright, my final solution below

grep -P --colour=always '(?:^|(?<=[\\, ;\-\+\*\/]))[-+]?[0-9]*[\.eE]?[0-9]+|$'

It looks behind for separation characters or beginning of line, and matches a great deal of number formats and the end of the line (so that every line is printed).

Finally, two useful aliases view and less-view:

alias v="grep --colour=always -P '(?:^|(?<=[\\, ;\-\+\*\/]))[-+]?[0-9]*[\.eE]?[0-9]+|$'"
function lv {                                                                    
    v $1 | less -R                                                               

Solution C

Here a better regex expression (to use within the single quotes of Solution B):


This solution accounts for any non alphanumeric and dot separation symbols. The previous one was lacking of generality.

Edit: adding -nT to grep shows aligned line numbers. Edit2: account for negative exponents by adding \-?.


Paste this in your ~/.bashrc and use the v, lv, cv to view, less-view, and column-view your text files:

# Highlight numbers when displaying text files
alias v="grep --colour=always -nTP '(?<![\w\.])[-+]?\-?[0-9]*[\.eE]?[0-9]+|$'"
# Send v output to less
function lv {
    v $1 | less -R
# Convert CSV to TSV and send to lv
function cv {
    column -ts, $1 | lv

Edit: add CSV file reading shortcut and comments.


Match also +/- signs and e as well.

 grep -E --color '[-+\.]?[0-9](|[eE][-+]?[0-9]|$)'

This will match below various of numbers.

  • 1
    or grep --color '[[:digit:]]' – Jeff Schaller Oct 12 '17 at 19:02
  • Yeah, more or less what I've suggested above. So, isn't there any default command, is it? – Atcold Oct 12 '17 at 19:04
  • You need to add |$ to print every line, though. – Atcold Oct 12 '17 at 19:06
  • Also you need to escape the \., otherwise you'll match everything. – Atcold Oct 12 '17 at 19:09
  • Also, I don't think you need the + at all. – Atcold Oct 12 '17 at 19:12

With vim:

:syn on
:syn match Number '\v[-+]?\d*\.?\d+([eE][-+]?\d+)?'

That's if you only want numbers (at least this format of numbers, you could also want to add 0x123, 1,23, 0x1ap-2, VII, 1,000,000, ...) highlighted. Many syntax highlighters shipped with vim for different languages will also highlight numbers as understood by the corresponding language.

If you do a:

grep -F '[eE' /usr/share/vim/vim*/syntax/*

you'll see all the languages that have some forms of numbers including ones with scientific notation highlighted by vim.

You may also want to decide in which context those numbers may be highlighted, like should the 1 be highlighted in ls.1, PS1, 1+2, -1-? Should 1e2 or 2e3 or none be highlighted in 1e2e3, etc?


Some other possibilities:

Colorize, color text

        0) reset-no-color (local), Emits sequence to remove (stray) color
           echo -e "\[\033[0;00m\]"

        1) coforma (local), color for match, add specified color for pattern

        2) coteli (local), insert ANSI color codes around line blocks,
           COlor TExt LInes

        3) spc, "supercat", colorize and print to standard output,
           general (but therefore complex) rules for coloring.
           Found in Debian repository and at:

        4) ansifilter, convert color codes, etc., to HTML, BBcode, others

        5) my-highlight (local)

        6) my-hilite (local)

        7) hh, easy highlighting, wrapper around ack

        8) pygmentize, parse and colorize program source file
           In Debian package python-pygments

For example, I ran:

spc data1

with a configuration file of .spcrc:

Cyan                 cya     t ([0-9]+)

to get:

Mike Brady 2
Carol 3.1415 Brady
77 Greg Brady
Marcia Brady

With digits in cyan - sorry, I don't see how to get the forum to render the color from BBCode or HTML or ... -- suggestions will be appreciated.

Best wishes ... cheers, drl

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.