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: Please note that the information provided on this website is intended for informational purposes only and does not represent a warranty. The opinions expressed are those of the author only. We recommend testing any solutions in a development environment before implementing them in production. The articles are based on our good faith efforts and were current at the time of writing, reflecting our practical experience 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

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

Ethereum:
Donate to Joel Lipman with Ethereum 0xb038962F3809b425D661EF5D22294Cf45E02FebF
© 2024 Joel Lipman .com. All Rights Reserved.