Page 1 of 1
[SOLVED]I need a function that deletes a single line in a...
Posted: 18 Jan 2011 05:03
by akol
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!
Re: I need a function that deletes a single line in a text f
Posted: 18 Jan 2011 08:01
by amel27
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
Re: I need a function that deletes a single line in a text f
Posted: 18 Jan 2011 08:12
by akol
amel27, thanks for fast reply, the code works great!
Re: [SOLVED]I need a function that deletes a single line in
Posted: 18 Jan 2011 08:38
by amel27
akol, sorry, "DisableDelayedExpansion" added,
otherwise exclamations cut out from text
Re: [SOLVED]I need a function that deletes a single line in
Posted: 18 Jan 2011 09:12
by akol
Oh, relax... thank you again!
Re: I need a function that deletes a single line in a text f
Posted: 18 Jan 2011 20:12
by ghostmachine4
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
Posted: 18 Jan 2011 23:17
by amel27
ghostmachine4 wrote:I wouldn't want to use it if my lines ever were to unknowingly contain a beginning "[".
leading "[" supported, only leading "]" stripped from line
Re: I need a function that deletes a single line in a text f
Posted: 19 Jan 2011 03:41
by ghostmachine4
amel27 wrote:ghostmachine4 wrote:I wouldn't want to use it if my lines ever were to unknowingly contain a beginning "[".
leading "[" supported, only leading "]" stripped from line
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
Posted: 19 Jan 2011 12:02
by akol
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 ".
Re: [SOLVED]I need a function that deletes a single line in
Posted: 19 Jan 2011 12:15
by aGerman
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:
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
Posted: 19 Jan 2011 12:45
by akol
aGerman, I agree and thank.
Any way, I thank all who contributed here.
Re: [SOLVED]I need a function that deletes a single line in
Posted: 19 Jan 2011 21:44
by ghostmachine4
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 windowsjust 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
Posted: 20 Jan 2011 20:47
by amel27
aGerman wrote:This is my proposal
Nice tip, aGerman, thanks. But your SUB required DisableDelayedExpansion too. If main script use EnableDelayedExpansion mode, exclamations cut out from text.
Re: [SOLVED]I need a function that deletes a single line in
Posted: 21 Jan 2011 13:18
by aGerman
Caught
Youre absolutely right.
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