mihail
Goto Top

Nummerierung jeder Zeile in einer Textdatei

Guten Tag sehr geehrten Admins,

Ich versuche seit einer Weile, als absoluter Anfänger eine batch-Datei zu finden die, wie das o.g. Thema es auch andeutet, welche die Zeilen in einer Textdatei einzeln nummeriert. Das Thema wurde in viele Foren im Internet diskutiert und ich bin schon mehrmals fündig in Internet geworden. Alles bassiert sich auf eine simple aber effiziente Methode:
Man erstellt eine 100%-eKopie der sogenannten "inputdatei" wobei mit einigen Befehle in der "Output-Datei" vorab der Zeilnnhalt jeder Zeile nummeriert wird. Leider alles was ich bis jezt ausprobiert habe zeigt mit den gleichen Fehler: die letzte Zeile wir immerwieder in der Outputdatei zusätzlich erneut kopiert.
D.h. hat man in der Input-Datei 1 Zeile wird diese in der Output-Datei verdoppelt und die neue entstandene Zeilen werden entsprechend mit 1 und 2 nummeriert, z.B.:

während die Datei Input.txt zwei Zeilen hat:
C162SMD08-S08G3xxxx#01S#089583-16072x0001
C162SMD08-S08G3xxxx#01S#089583-16072x0002

wird man im Output.txt wie schon beschrieben folgendes dargestellt:

1: C162SMD08-S08G3xxxx#01S#089583-16072x0001
2: C162SMD08-S08G3xxxx#01S#089583-16072x0002
3: C162SMD08-S08G3xxxx#01S#089583-16072x0002

Ich habe folgende Batch-Dateien schon getestet aber mit dem gleichen Ergebniss:

echo off
setlocal enabledelayedexpansion
SET infile=Input
SET outfile=Output

SET CNT=0
IF NOT EXIST "%Input%" GOTO ERROR
FOR /F "delims=" %%g IN (%Input%) DO (
SET line=%%g
CALL :INKR
)
:INKR
SET /A "CNT+=1"
IF "%CNT%" EQU "1" (ECHO %CNT%: %line% > %Output%) ELSE (ECHO %CNT%: %line% >> %Output%)
GOTO:EOF
:EOF
:ERROR
ECHO %Input% nicht gefunden!
:EOF

oder

echo off
setlocal enabledelayedexpansion
set /A z=0
FOR /F %%h IN (Input) DO (set /A z = "z+1" && echo %z%:%%h>>Output)

oder

echo off
setlocal enabledelayedexpansion
for /F "usebackq tokens=*" %%f in (`find /n /v "@@@@@"^< %Input%`) do @set line=%%f & set line=!line:]= !& set line=!line:[=! & echo !line!>> %Output%

Ich habe vieles selber ausprobiert aber ich finde den Fehler nicht.
Bitte, könnt Ihr mir evtl. hierzu behilflich sein?

Vielen Dank,

Miihail

Content-Key: 309945

Url: https://administrator.de/contentid/309945

Printed on: April 24, 2024 at 17:04 o'clock

Mitglied: 129813
129813 Jul 15, 2016 updated at 15:49:06 (UTC)
Goto Top
Hi,
why so much code for such a simple thing face-smile
findstr /n "^" "C:\file.txt" >"C:\file_numbered.txt"  
Regards
Member: Mihail
Mihail Jul 15, 2016 at 17:14:50 (UTC)
Goto Top
Hi highload,
you're right , it is simple for who knows face-smile. Thank you very much for your help.
The solution is very simple but has not the possibility to introduce at the beginning of each line a space .
I would be interested to know too why the other ideas have not worked. So one I can learn more about. Could you find this if you have time for that was not correct?
Greetings and thanks again,
Mitglied: 129813
129813 Jul 15, 2016 updated at 17:35:14 (UTC)
Goto Top
Zitat von @Mihail:
The solution is very simple but has not the possibility to introduce at the beginning of each line a space .
Also not that problem:
@echo off & setlocal
SET "infile=C:\input.txt"  
SET "outfile=C:\output.txt"  
>"%outfile%" (for /f "tokens=1* delims=:" %%a in ('type "%infile%" ^| findstr /n "^"') do echo %%a: %%b)  
I would be interested to know too why the other ideas have not worked. So one I can learn more about. Could you find this if you have time for that was not correct?
The reason is, you are using "delayedexpansion" but you still use percent signs inside the for loop, you have to use exclamationmarks "!" around variables that you declare inside the for loop. So if you set the counter CNT inside the loop you have to write !CNT! if you use its value.
Read here about delayed expansion.

Regards
Member: Mihail
Mihail Jul 15, 2016 at 18:45:46 (UTC)
Goto Top
Hi highload,
Great job . I am particularly impressed about. I hope I have learned something from it but I think I'll gain some experience only through practice.
Thanks a lot , you've helped a lot .
Best regards,and a good evening.