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?
To extract only lines containing digits:
$ grep '^[0-9][0-9]*$' words >digits
The regular expression
^[0-9][0-9]*$ will match any line which begins with a digit and then contains only digits until the end of line.
If your file doesn't have empty lines, you may change it to
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 '^[[: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
$ grep -E '^[+-]?([0-9]*\.)?[0-9]+$' words >floats
It all comes down to what kind of "number" we're looking for.
Delete any line containing letters by
sed and then put your output to another file:
sed '/[a-z]/Id' yourInputFile > yourOutputFile
OR if you want to change the original file in-place:
sed -i '/[a-z]/Id' yourInputFile