[SOLVED]I need a function that deletes a single line in a...
Moderator: DosItHelp
[SOLVED]I need a function that deletes a single line in a...
I need a function that deletes a single line in a text file.
Ex:
:delLine file linen
:: file -- file you want to modify.
:: linen -- line number you wish to delete.
...
<Help with this part, hahaha!>
Someone can help me.
Thanks!
Ex:
:delLine file linen
:: file -- file you want to modify.
:: linen -- line number you wish to delete.
...
<Help with this part, hahaha!>
Someone can help me.
Thanks!
Last edited by akol on 18 Jan 2011 08:13, edited 1 time in total.
Re: I need a function that deletes a single line in a text f
via native CMD, text lines must not begin with "]"
Code: Select all
@echo off
call:DelLine file.txt 10
exit
:DelLine %file% %line_no%
SETLOCAL DisableDelayedExpansion
set "$TMP=%TEMP%\%RANDOM%%RANDOM%.tmp"
(for /f "tokens=1* delims=]" %%a in ('^<%~1 find /v /n ""') do (
if not "%%a"=="[%~2" echo.%%b))>"%$TMP%"
copy "%$TMP%" "%~1"&& del "%$TMP%"
ENDLOCAL
GoTo:EOF
Last edited by amel27 on 18 Jan 2011 16:10, edited 2 times in total.
Re: I need a function that deletes a single line in a text f
amel27, thanks for fast reply, the code works great!
Last edited by akol on 18 Jan 2011 09:10, edited 1 time in total.
Re: [SOLVED]I need a function that deletes a single line in
akol, sorry, "DisableDelayedExpansion" added,
otherwise exclamations cut out from text
otherwise exclamations cut out from text
Re: [SOLVED]I need a function that deletes a single line in
Oh, relax... thank you again!
-
- Posts: 319
- Joined: 12 May 2006 01:13
Re: I need a function that deletes a single line in a text f
amel27 wrote:via native CMD, text lines must not begin with "]"
still, this is a "defect" in a software product. I wouldn't want to use it if my lines ever were to unknowingly contain a beginning "[".
A better way, is to set a counter, then iterate the file using the for loop as usual. When the counter reaches the line number specified by user, skip. otherwise, print.
Re: I need a function that deletes a single line in a text f
leading "[" supported, only leading "]" stripped from lineghostmachine4 wrote:I wouldn't want to use it if my lines ever were to unknowingly contain a beginning "[".
-
- Posts: 319
- Joined: 12 May 2006 01:13
Re: I need a function that deletes a single line in a text f
amel27 wrote:leading "[" supported, only leading "]" stripped from lineghostmachine4 wrote:I wouldn't want to use it if my lines ever were to unknowingly contain a beginning "[".
doesn't matter, either way, its a "defect" or a "limitation" of the batch. i sure hope OP doesn't have lines beginning with "]"
Re: [SOLVED]I need a function that deletes a single line in
Hey, don't worry. The lines of my document doesn't will begin with "]". They will begin with ", any problems?
--- EDITED ---
I tested and had no problem starting the lines with ".
--- EDITED ---
I tested and had no problem starting the lines with ".
Re: [SOLVED]I need a function that deletes a single line in
Yeah akol, regardless if you've got problems with this code, it's always a good idea to try make things better.
This is my proposal:
Regards
aGerman
This is my proposal:
Code: Select all
@echo off &setlocal
call :DelLine "file.txt" 10
goto :eof
:DelLine file_name line_no
>nul move "%~f1" "%temp%\%~n1.tmp" ||goto :eof
for /f "tokens=* delims=1234567890" %%a in ('type "%temp%\%~n1.tmp"^|findstr /n "^"') do (
set "line=%%a"
set /a n+=1
setlocal enabledelayedexpansion
if "!n!" neq "%~2" >>"%~f1" (echo(!line:~1!)
endlocal
)
goto :eof
Regards
aGerman
Re: [SOLVED]I need a function that deletes a single line in
aGerman, I agree and thank.
Any way, I thank all who contributed here.
Any way, I thank all who contributed here.
-
- Posts: 319
- Joined: 12 May 2006 01:13
Re: [SOLVED]I need a function that deletes a single line in
akol wrote:Hey, don't worry. The lines of my document doesn't will begin with "]". They will begin with ", any problems?
--- EDITED ---
I tested and had no problem starting the lines with ".
only if you are ABSOLUTELY certain that in the future, things will remain the same. Otherwise, you are better off not using "defected" software/methods.
if you can, use a proper file parsing tool such as gawk for windows
just a few line does what you want
Code: Select all
C:\test>more file
1 this is line 1
2 this is line 2
3 this is line 3
C:\test>set num=2
C:\test>gawk -vn=%num% "NR!=n" file
1 this is line 1
3 this is line 3
line number 2 is deleted.
Re: [SOLVED]I need a function that deletes a single line in
Nice tip, aGerman, thanks. But your SUB required DisableDelayedExpansion too. If main script use EnableDelayedExpansion mode, exclamations cut out from text.aGerman wrote:This is my proposal
Re: [SOLVED]I need a function that deletes a single line in
Caught Youre absolutely right.
What about this:
Regards
aGerman
What about this:
Code: Select all
:DelLine file_name line_no
setlocal disabledelayedexpansion
set /a n=0
if exist "%temp%\%~n1.tmp" del "%temp%\%~n1.tmp"
for /f "tokens=* delims=1234567890" %%a in ('findstr /p /n "^" "%~f1"') do (
set "line=%%a"
set /a n+=1
setlocal enabledelayedexpansion
if "!n!" neq "%~2" >>"%temp%\%~n1.tmp" (echo(!line:~1!)
endlocal
)
if %n%==0 (endlocal &exit /b 1)
>nul move /y "%temp%\%~n1.tmp" "%~f1" ||(endlocal &exit /b 1)
endlocal &exit /b 0
Regards
aGerman