Prefix Line Numbers in a Text File

Intro
Prefixing lines with their respective line number in a text file using a Microsoft Windows Operating System... I've just googled this as I couldn't remember how I did this last time and a number of people showing off their MS-DOS batch skills have proposed stupid extensive solutions when all you need is one command-line.


Why?
Working with programming languages, I often need to write the accompanying documentation. Within the documentation, I may want to refer to a line of code within a text file. I also find myself copying amounts of code into the same document and then needing lines prefixed so that I can explain the code.


What?
Change contents of "original_file.txt"
copyraw
The first line of my code
The second line of my code
The third line of my code
  1.  The first line of my code 
  2.  The second line of my code 
  3.  The third line of my code 
To "results_file.txt"
copyraw
1:  The first line of my code
  2:  The second line of my code
  3:  The third line of my code
  1.  1:  The first line of my code 
  2.    2:  The second line of my code 
  3.    3:  The third line of my code 


How?
  1. Open a MS-DOS Command Prompt (Start > Run... > Type "COMMAND" > Enter)
  2. [Optional] Browse to the folder that contains the file (or use full path in path reference). This example assumes the file is in C drive (C:\).
  3. Create an empty text file (Type "ECHO > TEMP.TXT")
  4. Type "FC /N /LB 300 c:\original_file.txt c:\temp.txt > c:\results_file.txt"
  5. The resulting file will be "c:\results_file.txt", open it in your favorite text editor... ta-daa...

Explanation:
  • FC is a file comparison tool in MS-DOS
  • /N tells the comparison tool to prefix each line with its respective number
  • /LB 300 tells it to buffer 300 lines (default will show first 100 lines - change this to the number of lines or greater in your file)
  • original_file.txt is your original file (obviously change the name to your file)
  • temp.txt is just a dummy file which should be empty for clarity
  • results_file.txt is the resulting file, it will be created in the current working directory.

Above and Beyond: Add to your Context Menu
My absolute laziness drives me to write the instructions on adding this to the context menu (right-click menu) when you right-click on a text file:
  1. Open the system registry (Start > Run... > Type "REGEDIT" > OK)
  2. Navigate to My Computer\HKEY_CLASSES_ROOT\txtfile\shell
  3. Right-click on "shell" and create a new key with the action name (eg. "Prefix_with_line_numbers")
  4. Right-click on this new key and add a key to this one called "command"
  5. Under the "command" key, double-click the "(Default)" string in the right-pane and type the following c:\windows\system32\cmd.exe /c "ECHO > c:\temp.txt | FC /N /LB 1000 %L c:\temp.txt > c:\results_file.txt | start c:\results_file.txt"
  6. Right-clicking on a text file now and selecting this option will generate a c:\results_file.txt with the line numbers prefixed and open it; the 1000 is the number of lines but you can increase/decrease this to the length of the files you usually do this to (put a higher number than the maximum length [=number of lines] of the files you will be using this function on).
What you should end up with:
RegEdit Keys, Strings and Values
copyraw
-- yields

Comparing files H:\temp.txt and C:\TEMP.TXT
***** H:\temp.txt
    1:  This is line 1
    2:  This is line 2
    3:  This is line 3
***** C:\TEMP.TXT
    1:  ECHO is on.
*****
  1.  -- yields 
  2.   
  3.  Comparing files H:\temp.txt and C:\TEMP.TXT 
  4.  ***** H:\temp.txt 
  5.      1:  This is line 1 
  6.      2:  This is line 2 
  7.      3:  This is line 3 
  8.  ***** C:\TEMP.TXT 
  9.      1:  ECHO is on. 
  10.  ***** 

Additional
  • You could change c:\results_file.txt to %TEMP%\results_file.txt to send the file to your temporary directory instead of using the root c drive.
  • If you do not have c:\windows folder than find where "cmd.exe" exists and chage the path in the above instructions accordingly.
  • The use of some environment variables (eg. %ComSpec% or %windir%) in the command may cause an "Access Denied" error on some networks. Use the hardcoded path.
Category: MS-DOS :: Article: 419

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: The information on this website is provided without warranty and any content is merely the opinion of the author. Please try to test in development environments prior to adapting them to your production environments. The articles are written in good faith and, at the time of print, are working examples used in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com

Related Articles

Joes Revolver Map

Joes Word Cloud

root   start   command   length   within   prefixed   above   windows   echo   results   create   open   instructions   text   should   menu   line   need   comparison   number   path   tool   tells   drive   shell   lines   file   context   first   type   prefix   empty   using   than   files   name   1000   change   click   third   folder   resulting   temp   code   right   original   numbers   directory   default   second   JoelLipman.Com

Accreditation

Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin - Valid till 8 May 2022 3QnhmaBX7LQSRsC9hh6Je9rGQKEGNQNfPb
© 2021 Joel Lipman .com. All Rights Reserved.