Delete all lines of text that isn’t all numbers

greptext processing

I have a wordlist that I'd like to extract every line of text from that contains only numbers, to a new file. What do I do?

Best Answer

To extract lines containing only digits:

$ grep -x '[0-9][0-9]*' words >digits

The regular expression [0-9][0-9]* will match any line contains at least one digit, and the -x option to grep requires that the whole line matches the given expression.

If your file doesn't have empty lines, you may change it to grep -x '[0-9]*'.

If you want lines with numbers, delete lines with alphabetic characters instead (easier than trying to construct a regular expression for a generic number):

$ grep -v '[a-zA-Z]' words >numbers

Both variants using POSIX character classes:

$ grep -x '[[:digit:]][[:digit:]]*' words >digits
$ grep -v '[[:alpha:]]' words >numbers

Update: If you want to select the lines containing floating point numbers you could use the (extended) regular expression ^[+-]?([0-9]*\.)?[0-9]+$:

$ grep -x -E '[+-]?([0-9]*\.)?[0-9]+' words >floats

It all comes down to what kind of "number" we're looking for.

Related Question