110

I have file like this :

 other lines . . .    
 blah blah blah (:34)

I wish to find the occurrence of numbers in the above file. I came up with:

grep [0-9] filename

But that is printing the whole:

blah blah blah (:34)

Rather I want only 34. Is there any way to do so?

2
  • In the future, also check out the man page for grep (or any other program). The man page details the options required for many common uses of the program. e.g. man grep
    – hnasarat
    Sep 5, 2012 at 4:33
  • You can try this >grep -o '[0-9][0-9]*' testfile
    – user159512
    May 17, 2013 at 10:18

5 Answers 5

155

You can use grep -E to access the extended regular expression syntax( Same as egrep)

I have created a testfile with below contents:

>cat testfile
this is some text
with some random lines

again some text
ok now going for numbers (:32)
ok now going for numbers (:12)
ok now going for numbers (:132)
ok now going for numbers (:1324)

Now to grep the numbers alone from the text you can use

>grep -Eo '[0-9]{1,4}' testfile
32
12
132
1324

will be output.

Here "-o" is used to only output the matching segment of the line, rather than the full contents of the line.

The squiggly brackets (e.g. { and }) indicate the number of instances of the match. {1,4} requires that the previous character or character class must occur at least once, but no more than four times.

Hope this helps

5
  • 5
    Nice. Also, to match more than 4 or arbitrary number of digits, use grep -Eo '[0-9]{1,}' testfile Aug 13, 2014 at 19:32
  • @FractalSpace when have the constrain of repeating at least one time in [0-9]{1,}, the -o option might not necessary, since repeating at least one time, by definition remove the empty output?
    – Karl Xu
    Mar 26, 2021 at 8:25
  • It's been a while, so I have no recollection of what I wrote and why :) Mar 26, 2021 at 21:06
  • @FractalSpace You can also write grep -Eo "[0-9]+", which is a little more concise.
    – gtcaz
    Dec 16, 2022 at 2:08
  • Shorter still would be grep -Po "\d+" using PCRE.
    – gtcaz
    Dec 16, 2022 at 2:16
22

You can use RE bracket expression [:digit:] specified by section 9.3.5 of POSIX standard , in combination with -o flag to print only matching "words"

$ grep -o '[[:digit:]]*' <<< $'No number in this line\nbut 123 here'                                                     
123
18

You can use Perl style regular expressions as well

grep -Po "\\d+" filename

-P Interpret PATTERNS as Perl-compatible regular expressions (PCREs).

-o Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.

3
  • deserves way more than 2 upvotes :)
    – alec
    Jan 26, 2021 at 2:09
  • 1
    Note: You may not have -P option if running BSD grep (e.g. OSX. )
    – spuder
    Jan 9, 2022 at 20:15
  • Why two backslashes? Wouldn't that escape the backslash?
    – gtcaz
    Dec 16, 2022 at 2:29
7

grep -o will print only the matching part of the line. Otherwise grep will print any lines with the pattern.

1

I would use curl to access your file locally or remotely, then I would grep lines with numbers wrapped in (: ) then cut those pieces off and write to file

the accepted answer ignores there could be numbers in the previous lines of the file, it does work for the example data, but what if the file was remote?

Local

curl file:///home/$USER/Public/input.txt  | grep -o '(:.*)' | cut -d ":" -f 2 | cut -d ")" -f 1 > output.txt

In this example output.txt in your current folder will be written over, we are accessing input.txt from your Public folder.

Remote

curl https://yoursite.com/Public/input.txt  | grep -o '(:.*)' | cut -d ":" -f 2 | cut -d ")" -f 1 > output.txt

In this example output.txt in your current folder will be written over, we are accessing input.txt from https://yoursite.com/Public/.

2
  • 2
    Why use curl when a simple cat would do?
    – PerlDuck
    Jul 28, 2018 at 12:37
  • The question doesn't clearly state that the file is local. this answer allows for both.
    – Stef
    Jul 28, 2018 at 20:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .