Removing line numbers from text file

To reuse code from an OCR’d book or a text file that has line numbers embedded in the file, say with example file neat.py:

1 # cool program
2 import sys
3 def howneat():
4     sys.exit('Thanks for visiting')

You can remove the line numbers by typing in Terminal:

perl -pe 's/^[ t]+d+//' neat.py  > neater.py

The regular expression

^[ t]+d+

\^ at the beginning of the line

[ t]+ match space or tab (one or more)

d+ match one or more digits

-p implicitly loop over (read each line of) the file neat.py.

-e here is the PCRE to consider.

And we replace with nothing. This leaves alone the indentation of the code (relevant for Python).

I could have used the -i flag to edit in place, but I instead redirected STDOUT to the file neater.py in case I made a mistake (inputting the wrong file, for example).

Categories:

Updated: