3

Okay, I know this is purely academic because the findstr command can do what I expect in a jiffy.

I have a text file whose strings (lines) I want numbered. I was wondering whether it's possible to write a batch that emulates it and gives me the flexibility of moving the tagging integer anywhere in the string.

For example the standard output is 1: some string

Let us say I want

  • 1)some string (without using find and replace in Windows Explorer), or
  • somestring........(1), or
  • s 1] somestring
Mofi
  • 46,139
  • 17
  • 80
  • 143
C_urious' '
  • 101
  • 2
  • 5
  • 1. `/n` is not possible with pure batch token parsing via `for /f` as it skips empty lines so you'll have to use some external tool like `find` to add the numbers to the empty lines. 2. Fully implementing `/r` - regular expressions - is unrealistic. – wOxxOm Jul 26 '15 at 09:38
  • check this - http://stackoverflow.com/a/8844873/388389 – npocmaka Jul 26 '15 at 10:20

2 Answers2

1

EDIT: I modified the code in order to insert the line number at any place in the line:

@echo off
setlocal DisableDelayedExpansion

rem Define the format of output lines; for example

rem To show "(lineNo) line" use:
set "format=(!lineNo!) !line!"

rem To show "20 chars of line (lineNo) rest of line" use:
set "format=!line:~0,20! (!lineNo!) !line:~20!"
rem and adjust each line read appending 20 spaces

rem To show "line (lineNo placed at column 75)" use:
set "format=!line:~0,73! (!lineNo!)"
rem and adjust each line read appending 73 spaces

rem To show line numbers with left zeros, start lineNo with the proper
rem number of zeros and modify the format accordingly; for example:
set lineNo=100
set "format=!line:~0,73! (!lineNo:~1!)"


setlocal EnableDelayedExpansion
copy %1 "%~1.tmp" > NUL
set "EOF=%random%%random%%random%"
echo :%EOF%EOF:>> "%~1.tmp"
REM set lineNo=0
call :ReadLines < "%~1.tmp"
del "%~1.tmp"
goto :EOF

:ReadLines
   set "line="
   set /P "line="
   if "!line!" equ ":%EOF%EOF:" goto :EOF
   set "line=!line!                                                                         "
   set /A lineNo+=1
   echo %format%
goto ReadLines

Output example:

@echo off                                                                 (01)
setlocal DisableDelayedExpansion                                          (02)
                                                                          (03)
rem Define the format of output lines; for example                        (04)
                                                                          (05)
rem To show "(lineNo) line" use:                                          (06)
set "format=(!lineNo!) !line!"                                            (07)
                                                                          (08)
rem To show "20 chars of line (lineNo) rest of line" use:                 (09)
set "format=!line:~0,20! (!lineNo!) !line:~20!"                           (10)
rem and adjust each line read appending 20 spaces                         (11)
                                                                          (12)
rem To show "line (lineNo placed at column 75)" use:                      (13)
set "format=!line:~0,73! (!lineNo!)"                                      (14)
rem and adjust each line read appending 73 spaces                         (15)
                                                                          (16)
rem To show line numbers with left zeros, start lineNo with the proper    (17)
rem number of zeros and modify the format accordingly; for example:       (18)
set lineNo=100                                                            (19)
set "format=!line:~0,73! (!lineNo:~1!)"                                   (20)
                                                                          (21)
                                                                          (22)
setlocal EnableDelayedExpansion                                           (23)
copy %1 "%~1.tmp" > NUL                                                   (24)
set "EOF=%random%%random%%random%"                                        (25)
echo :%EOF%EOF:>> "%~1.tmp"                                               (26)
REM set lineNo=0                                                          (27)
call :ReadLines < "%~1.tmp"                                               (28)
del "%~1.tmp"                                                             (29)
goto :EOF                                                                 (30)
                                                                          (31)
:ReadLines                                                                (32)
   set "line="                                                            (33)
   set /P "line="                                                         (34)
   if "!line!" equ ":%EOF%EOF:" goto :EOF                                 (35)
   set "line=!line!                                                       (36)
   set /A lineNo+=1                                                       (37)
   echo %format%                                                          (38)
goto ReadLines                                                            (39)
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • When I tested your code it made command processor work furiously to reach the maximum value of %random% .IOW it was numbering blank lines when it should have stopped at around 400. – C_urious' ' Jul 28 '15 at 14:42
  • Did you inserted the file to process in the first parameter of the Batch file? Like in: `test.bat friends.txt` – Aacini Jul 28 '15 at 16:44
  • I modified my solution in order to insert the line number at any place in the line... – Aacini Jul 28 '15 at 18:16
  • Hehehe! you thought I wouldn't know that ? Why do you need randomness when you can determine the eof accurately ? Okay even if we pretend that we can't estimate the exact count of lines,something must be done to limit the randomness of %random% .I get ridiculously high figures even for text files that contain less than 100 strings ! It would be a colossal waste of paper if I decided to print the text file. – C_urious' ' Jul 29 '15 at 07:31
  • I am afraid I don't follow you... The random numbers are just used to mark the end of the file and have _no relation_ with the number of lines in the file. You may change such `set "EOF=%random%%random%%random%"` by `set "EOF=any other string"`; just be sure that the string does NOT appear in the file! There is _no other way_ to get the number of lines in a file without `find` or `findstr`; a simple `for /F ...` command (like in your "solution") delete all empty lines. **Note**: did you _found any problem_ with the result produced by my code? If so, please post it, so I can fix it... **`:/`** – Aacini Jul 29 '15 at 17:32
  • First off my focus was on the formatting of the strings and not the total number of strings in their file,so using find and not findstr to determine the number lines(strings) was kosher ! In any case I will post the result of using your code on my sample test below. – C_urious' ' Jul 30 '15 at 07:05
  • Well ! this is the bug in your code (01) this text file doesn't have many lines (02) yet executing your code results (03) in an output like this (04) I suppose people would want the numbering to terminate here:1489924529230 (05) (06) (07) (08)But it doesn't – C_urious' ' Jul 30 '15 at 07:08
0
 @echo off
SET SNO=1
FOR /f "tokens=*" %%X IN (friends.txt) DO (call :subroutine %%X)
GOTO :eof

:subroutine
:: This is the interesting part where the value of the SNO can be
:: moved around.If you want the SNO bang in the middle then
:: then that that's another project ! Moreover moving it to the end
:: is problematic if the strings don't have the same number 
:: of characters. The result is not visually pleasant in that case
echo.
echo (%SNO%)  %1>>numbered.txt
set /a SNO+=1
GOTO :eof 
C_urious' '
  • 101
  • 2
  • 5