sed or tr one-liner to delete all numeric digits

11,567

Solution 1

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

(\+ is a GNU extension; with some sed implementations, you may need the longer standard alternative: \{1,\} or use [0-9][0-9]*).

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

Solution 2

I believe what you are looking for is:

tr -d 0-9
Share:
11,567

Related videos on Youtube

Matthew
Author by

Matthew

Updated on September 17, 2022

Comments

  • Matthew
    Matthew over 1 year

    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?

  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' about 13 years
    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.
  • Matthew
    Matthew about 13 years
    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.