Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Linenumbering/de-numbering script

8 views
Skip to first unread message

Ritchie

unread,
May 2, 2003, 8:40:11 AM5/2/03
to
Thanks to Garry Deane, I've put this numbering/de-numbering script together.
Its fairly self-explanatory. I recommend that its saved as a .CMD file
rather than a .BAT file (remember the two are NOT created equally).

Each line of the script has been prefixed with its line number followed by
a period and one space. This all needs removing to use the script. Obviously
you can't use the script to do that, but you could use the method suggested
by Phil Robyn in this post (thanks Phil):-

http://groups.google.com/groups?selm=OtS3l4%24DDHA.1552%40TK2MSFTNGP12.phx.gbl

If reading this via Google, the script may have been mangled. Clicking the
Google 'Original Format' link will minimize the 'manglation' (new word?).
BTW, there are NO tab characters or trailing spaces in the script.

--
Ritchie

001. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
002. :;LN.CMD v1.0. Ritchie Lawrence, 2003-05-02.
003. :;Text file line numbering and de-numbering shell script for NT/2K/XP.
004. :;Includes tweak by Garry Deane to add NT compatability. Numbered and
005. :;de-numbered output is sent to stdout, errors to stderr. Script sets
006. :;errorlevel to zero if successful else non-zero, see script for details.
007. :;
008. :; Usage: LN.CMD <infile>
009. :;
010. :; <infile> Input file to be numbered/de-numbered
011. :;
012. :; Eg. LN.CMD temp.txt > "my batch.cmd"
013. :;
014. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
015. @echo off & setlocal ENABLEEXTENSIONS
016.
017. :: Display help if required, then exit with errorlevel 1
018. if "%1"=="" call :ShowUsage & md;2>nul & goto :EOF
019. if "%1"=="/?" call :ShowUsage & md;2>nul & goto :EOF
020.
021. :: if infile cannot be read, exit with errorlevel 2
022. type %1 >nul 2>&1 || (
023. >&2 echo/Error: Failed to open input file.
024. ftype/ 2>nul & goto :EOF
025. )
026.
027. :: if infile contains ASCII 172 then exit with errorlevel 3
028. findstr ¬ %1 >nul && (
029. >&2 echo/Error: Input file cannot be reliably numbered/de-numbered
030. >&2 echo/by this script because it contains ASCII character 172.
031. con:: 2>nul & goto :EOF
032. )
033.
034. :: Decide whether to number or de-number the infile
035. call :IsNumbered %1 && call :LineDeNumber %1 || call :LineNumber %1
036.
037. endlocal & ver>nul & goto :EOF
038.
039. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
040. :ShowUsage
041. ::
042. :: Func: Prints to stderr, lines in this file that begin with a colon
043. :: followed by a semi-colon. For NT4/2K/XP.
044. ::
045. :: Args: None
046. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
047. setlocal ENABLEEXTENSIONS
048. for /f "tokens=1* delims=:;" %%a in ('findstr/b /n /c:":;" %~f0') do (
049. >&2echo/%%b
050. )
051. endlocal & goto :EOF
052. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
053.
054. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
055. :LineNumber %filename%
056. ::
057. :: By: Ritchie Lawrence, 2003-05-02 v1.1. This version includes a tweak
058. :: by Garry Deane to add NT compatability.
059. ::
060. :: Func: Prints the contents of %1 to stdout. Each line is prepended with a
061. :: zero padded line number followed by a period. For NT4/2K/XP.
062. ::
063. :: Args: %1 file to be numbered (by val)
064. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
065. setlocal ENABLEEXTENSIONS
066. for /f "tokens=1* delims=:" %%a in ('find /c /v "" ^< %1') do (
067. set pad=%%a123456
068. )
069. set /a i=0, pad=%pad:~6,1%
070. for /f "tokens=* delims=0123456789" %%b in ('findstr /n $ %1') do (
071. set "line=%%b"
072. set /a i+=1,lnum=1000000+i
073. call set lnum=%%lnum:~%pad%%%
074. call :LineNumber.Print
075. )
076. goto :EOF
077.
078. :LineNumber.Print
079. set "dvar=%line:"=¬%"
080. set "dvar=%dvar:^=^^%"
081. set "dvar=%dvar:&=^&%"
082. set "dvar=%dvar:<=^<%"
083. set "dvar=%dvar:>=^>%"
084. set "dvar=%dvar:(=^(%"
085. set "dvar=%dvar:)=^)%"
086. set "dvar=%dvar:|=^|%"
087. set "dvar=%dvar:¬="%"
088. echo:%lnum%. %dvar:~1%
089. endlocal & goto :EOF
090. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
091.
092. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
093. :LineDeNumber %filename%
094. ::
095. :: By: Ritchie Lawrence, 2003-05-02 v1.0.
096. ::
097. :: Func: Prints the contents of %1 to stdout. Each line is stripped of any
098. :: leading digits and the next two characters. For NT4/2K/XP.
099. ::
100. :: Args: %1 file to be de-numbered (by val)
101. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
102. setlocal ENABLEEXTENSIONS
103. for /f "tokens=1* delims=0123456789" %%a in ('findstr /n $ %1') do (
104. set "line=%%b"
105. call :LineDeNumber.Print
106. )
107. goto :EOF
108.
109. :LineDeNumber.Print
110. set "dvar=%line:"=¬%"
111. set "dvar=%dvar:^=^^%"
112. set "dvar=%dvar:&=^&%"
113. set "dvar=%dvar:<=^<%"
114. set "dvar=%dvar:>=^>%"
115. set "dvar=%dvar:(=^(%"
116. set "dvar=%dvar:)=^)%"
117. set "dvar=%dvar:|=^|%"
118. set "dvar=%dvar:¬="%"
119. echo:%dvar:~2%
120. endlocal & goto :EOF
121. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
122.
123. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
124. :IsNumbered %filename%
125. ::
126. :: By: Ritchie Lawrence, 2003-05-02 v1.0.
127. ::
128. :: Func: Determines if input file has already been numbered or not. Returns
129. :: true if each line starts with digits followed by two characters
130. :: that are the same for each line, otherwise returns false. For
131. :: NT4/2K/XP.
132. ::
133. :: Args: %1 file to be analysed (by val)
134. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
135. setlocal ENABLEEXTENSIONS & set "last=" & set passed=1
136. for /f %%a in ('find /c /v "" ^< %1') do set TotalLines=%%a
137. for /f "delims=:" %%a in ('findstr /n /b [0-9] %1') do (
138. set NumberedLines=%%a
139. )
140. if not "%TotalLines%"=="%NumberedLines%" (endlocal & md;2>nul & goto :EOF)
141. for /f "tokens=1* delims=0123456789" %%a in ('findstr $ %1') do (
142. set line=%%a & call :IsNumbered.Check
143. )
144.
145. if %passed%==1 (
146. endlocal & ver>nul & goto :EOF
147. ) else (
148. endlocal & md;2>nul & goto :EOF
149. )
150.
151. :IsNumbered.Check
152. set "dvar=%line:"=¬%"
153. set this="%dvar:~0,2%"
154. if defined last if not %this%==%last% set passed=0
155. set last=%this%
156. goto :EOF
157. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


Garry Deane

unread,
May 3, 2003, 12:52:37 AM5/3/03
to
On Fri, 2 May 2003 13:40:11 +0100, "Ritchie"
<rlaw...@commanddoline.co.uk> wrote:

>Thanks to Garry Deane, I've put this numbering/de-numbering script together.
>Its fairly self-explanatory. I recommend that its saved as a .CMD file
>rather than a .BAT file (remember the two are NOT created equally).
>
>Each line of the script has been prefixed with its line number followed by
>a period and one space. This all needs removing to use the script. Obviously
>you can't use the script to do that, but you could use the method suggested
>by Phil Robyn in this post (thanks Phil):-
>
>http://groups.google.com/groups?selm=OtS3l4%24DDHA.1552%40TK2MSFTNGP12.phx.gbl
>
>If reading this via Google, the script may have been mangled. Clicking the
>Google 'Original Format' link will minimize the 'manglation' (new word?).
>BTW, there are NO tab characters or trailing spaces in the script.

Very nice work except for the serious error of misspelling
"compatability" <grin>. Appears to work a treat.

Garry

0 new messages