Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So, I have this textfile, and it consists of mostly alphanumeric characters. It's a standard document. But since I copied it and pasted it from a PDF, there are page numbers in there. I don't much care for the occasional number that's not a page, so I figure I'll wipe them all out with sed or tr. Just marginally faster than find and replacing first zero, then one, then two, etc. in the GUI, after all.

So how do I do that?

share|improve this question
up vote 6 down vote accepted

To remove all digits, here are a few possibilities:

tr -d 0-9 <old.txt >new.txt
tr -d '[:digit:]' <old.txt >new.txt
sed -e 's/[0-9]//g' <old.txt >new.txt

If you just want to get rid of the page numbers, there's probably a better regexp you can use, to recognize just those digits that are page numbers. For example, if the page numbers are always alone on a line except for whitespace, the following command will delete just the lines containing nothing but a number surrounded by whitespace:

sed -e '/^ *[0-9]\+ *$/d' <old.txt >new.txt

You don't need to use the command line for this, though. Any halfway decent editor has regexp search and replacement capabilities.

share|improve this answer
What about sed -ibak -e 's/[0-9]//g' file.txt? – D.Shawley Mar 7 '11 at 19:27
gotta love that tr syntax. whoever wrote that program was a kindred spirit, i imagine, because i personally have always found sed and grep eyesores. i know i don't have to use the command line. these days i automatically revert to CL after a certain number of clicks (maybe 3) is required for something because i know later on it will be useful to me to know how to do so... but thanks for the sed command you give at the end because honestly i would love to have mastered regex and it would serve me greatly and of all the commands above it's actually the one that i will end up using. – ixtmixilix Mar 21 '11 at 21:12

I believe what you are looking for is:

tr -d 0-9
share|improve this answer
tr -d '[0-9]' would remove brackets as well. tr -d [0-9] usually removes brackets and digits, but may do something else if there is a file whose name is a single digit in the current directory. – Gilles Mar 7 '11 at 1:06
@Gilles, Oops, sure & thanks. – asoundmove Mar 7 '11 at 3:08

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.