In windows it is possible to write a batch that emulates findstr / n / r. file.txt?

Okay, I know this is purely academic because the team findstr

can do what I expect in jiffy.

I have a text file whose lines (lines) I want to number. I was wondering if it was possible to write a batch that mimics it and gives me the flexibility to move the chain of labels anywhere in the line.

For example standard output 1: some string

Let's say I want

  • 1)some string

    (without using find and replace in Windows Explorer) or
  • somestring........(1)

    , or
  • s 1] somestring

+3


source to share


2 answers


EDIT : I changed the code to insert the line number anywhere 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)

      

+1


source


 @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 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 

      



0


source







All Articles