Tuesday, July 29, 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

  • Joes Quicklist Weblinks (JQW)

    • Thu 17-Jul-14
      Hi, thanks a lot. But I have got a problem with download it. Error: "File could not be found Important!
      Kat  
    • Tue 15-Jul-14
      Hi Artur, I forgot to get back to you sooner. I was going to include a set of instructions to do this ...
      Webmaster  
    • Mon 14-Jul-14
      Hi Skarosg, Not sure if we took this to e-mail but I noticed your site is displaying the images now.
      Webmaster  
    • Fri 11-Jul-14
      Hi, How can I put user name and category of weblink in module, near the date? Thanks..
      Artur
  • Joes Word Cloud (JWC)

    • Tue 17-Jun-14
      I'm using JE 3.1.19 stable.
      alve89