Wednesday, April 16, 2014
   
Text Size
Login

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"

  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"

  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

  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.

Comments   

aud2093
# aud2093 Mon, 18th March 2013
Great information, thanks! I'm still a fan of command line processing & DOS batch jobs for client task automation.
Like | Dislike | 0 Reply | Reply with quote | Quote
Add Comment

Name:

Email:

Website:

Message:


Latest Posts

  • 301 Redirect using htaccess file

    • Tue 15-Apr-14
      Further suggestion:
      RewriteRule ^(.*/)?assets/s 2dmain.html\?/( .*/)? $2 [R=301,L]
      Webmaster  
    • Thu 10-Apr-14
      Playing with some RegEx testers
      RewriteRule ^(.*\?/)?(?:$|( .+?)(?:(\.[^.]$ )|$)) $2 [R=301,L] ...
      Webmaster  
    • Wed 09-Apr-14
      I tried to redirect links from my old site to my new site, based on what I read in this thread. The old ...
      pelle
  • JComments 2.3.0 with ReCaptcha in Joomla 2.5.x

    • Fri 28-Mar-14
      You are a rockstar mate! thanks. Followed the steps listed and it worked! If only all tutorials were ...
      Kman
  • K2 Items disappear

    • Thu 03-Apr-14
      The fix works great, but the problem is occurring a couple of times a day. Any idea how to fix ...
      Larry C.