[SOLVED]I need a function that deletes a single line in a...

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
akol
Posts: 7
Joined: 18 Jan 2011 04:47

[SOLVED]I need a function that deletes a single line in a...

#1 Post by akol » 18 Jan 2011 05:03

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. :roll:
Thanks! :oops:
Last edited by akol on 18 Jan 2011 08:13, edited 1 time in total.

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: I need a function that deletes a single line in a text f

#2 Post by amel27 » 18 Jan 2011 08:01

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.

akol
Posts: 7
Joined: 18 Jan 2011 04:47

Re: I need a function that deletes a single line in a text f

#3 Post by akol » 18 Jan 2011 08:12

amel27, thanks for fast reply, the code works great!
Last edited by akol on 18 Jan 2011 09:10, edited 1 time in total.

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: [SOLVED]I need a function that deletes a single line in

#4 Post by amel27 » 18 Jan 2011 08:38

akol, sorry, "DisableDelayedExpansion" added,
otherwise exclamations cut out from text

akol
Posts: 7
Joined: 18 Jan 2011 04:47

Re: [SOLVED]I need a function that deletes a single line in

#5 Post by akol » 18 Jan 2011 09:12

Oh, relax... thank you again!

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: I need a function that deletes a single line in a text f

#6 Post by ghostmachine4 » 18 Jan 2011 20:12

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.

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: I need a function that deletes a single line in a text f

#7 Post by amel27 » 18 Jan 2011 23:17

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

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: I need a function that deletes a single line in a text f

#8 Post by ghostmachine4 » 19 Jan 2011 03:41

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 "]"

akol
Posts: 7
Joined: 18 Jan 2011 04:47

Re: [SOLVED]I need a function that deletes a single line in

#9 Post by akol » 19 Jan 2011 12:02

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 ".

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: [SOLVED]I need a function that deletes a single line in

#10 Post by aGerman » 19 Jan 2011 12:15

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

akol
Posts: 7
Joined: 18 Jan 2011 04:47

Re: [SOLVED]I need a function that deletes a single line in

#11 Post by akol » 19 Jan 2011 12:45

aGerman, I agree and thank.
Any way, I thank all who contributed here. :D

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: [SOLVED]I need a function that deletes a single line in

#12 Post by ghostmachine4 » 19 Jan 2011 21:44

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.

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: [SOLVED]I need a function that deletes a single line in

#13 Post by amel27 » 20 Jan 2011 20:47

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.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: [SOLVED]I need a function that deletes a single line in

#14 Post by aGerman » 21 Jan 2011 13:18

Caught :oops: 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

Post Reply