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?
You can use
grep -E
to access the extended regular expression syntax( Same as egrep)I have created a testfile with below contents:
Now to grep the numbers alone from the text you can use
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
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"You can use Perl style regular expressions as well
-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.
grep -o
will print only the matching part of the line. Otherwise grep will print any lines with the pattern.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
In this example
output.txt
in your current folder will be written over, we are accessinginput.txt
from your Public folder.Remote
In this example
output.txt
in your current folder will be written over, we are accessinginput.txt
fromhttps://yoursite.com/Public/
.