How to delete line of code 2nd time offending string found

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
treefrog
Posts: 2
Joined: 12 May 2012 02:06

How to delete line of code 2nd time offending string found

#1 Post by treefrog » 12 May 2012 02:39

Hi, I'm a newbie batch scripter and need some help. I have a file that needs to be searched for a string, which might take part and/or all of a line, and when that string is detected the *second time*, that line instance needs to be deleted; note that the line cannot be blank, but must be deleted. I've attempted to code it, but I'm running into errors. The string is "master_console^T=s^T;", without quotes, and where the ^T is a tab. Searching for "master_console" at the start of each line would be plenty, though, since that is unique enough.

The errors I've run into when attempting to code this are:
1) Passing a variable from a FOR /F "delims=" %%A statement via CALL is only passing the value up to the first space, and I need the whole line passed:
FOR /F "delims=" %%A in (file.txt) do CALL :SUB %%A
:SUB
SET _var1=%1

So if the line is "hello there", it is only passing "hello" to SUB.

2) Trying to capture the value of %%A from the above FOR statement into a variable so I can determine if it contains the string in question is not working.
FOR /F "delims=" %%A in (file.txt) do (
SET _var1=%%A)

That little routine isn't letting me assign the value of %%A into a variable. Doesn't matter if I have enabledelayedexpansion or ENABLEEXTENSIONS set. I'm trying to capture that value so I can search for the string mentioned above ("master_console") and delete that 2nd instance of the line in the file.txt. And the second time that string is detected, its line must be deleted - the line cannot be a carriage return.


I am hoping that if someone can help me get past one of these issues I can complete the script. I've checked the sample scripts and scriptlets on this and other sites, but they either don't contain the logic/syntax I need and/or they're too complex for me to dissect. For example, the BatchSubstitute.bat might have something helpful, but I haven't the foggiest clue how most of it works (it looks like someone magically crammed about 150 batch lines into 8! Argh! Help... "Momma..."

Regards,
globalfrog

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

Re: How to delete line of code 2nd time offending string fou

#2 Post by aGerman » 12 May 2012 05:01

Try whether something like that would work for you:

Code: Select all

@echo off
set "inFile=file.txt"
set "outFile=out.txt"

setlocal EnableDelayedExpansion
set /a n=0
<"!inFile!" >"!outFile!" (
  for /f %%i in ('type "!inFile!"^|find /c /v ""') do for /l %%j in (1 1 %%i) do (
    set "ln=" &set /p "ln="
    if "!ln:~0,14!"=="master_console" (
      set /a n+=1
      if !n! neq 2 echo(!ln!
    ) else (
      echo(!ln!
    )
  )
)

if !n! neq 2 means that only the second appearance of "master_console" would be ignored. Perhaps you should replace neq with lss.

Regards
aGerman

treefrog
Posts: 2
Joined: 12 May 2012 02:06

Re: How to delete line of code 2nd time offending string fou

#3 Post by treefrog » 12 May 2012 12:36

Hi aGerman,

Dude! That is way cool! It's perfect! I just wish I could understand what you did and how. Is there a place or book I can find out the syntax for the methods used? Even the method used to SET the variables is new to me. And you did it and replied in 2 hours. I am stunned.

Thank you!!!!! If there's a donation box please let me know where it is!
- treefrog

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

Re: How to delete line of code 2nd time offending string fou

#4 Post by aGerman » 12 May 2012 14:06

treefrog wrote:I just wish I could understand what you did and how.


I see. In that case I should explain a bit more.

Code: Select all

<"!inFile!" >"!outFile!" (
...
)

As you can see the piece of code that does the job is enclosed in parentheses. Both the incoming and the outgoing file are redirected to/from that code block. In- and out-stream are buffered. That enables SET /P to read line by line (instead of only the first line of a file) and it enables the > operator to redirect the entire file stream.


Code: Select all

  for /f %%i in ('type "!inFile!"^|find /c /v ""') do for /l %%j in (1 1 %%i) do (

FIND returns the number of lines in the file and the FOR /F loop assignes it to variable %%i.
The FOR /L loop uses %%i and loops exactly as many iterations as lines are in the incoming file.


Code: Select all

    set "ln=" &set /p "ln="

First the former value of variable ln is destroid. Then SET /P reads the next line from the file stream and assignes it to ln.


Code: Select all

    if "!ln:~0,14!"=="master_console" (
      set /a n+=1
      if !n! neq 2 echo(!ln!
    ) else (
      echo(!ln!
    )

That's quite simple, isn't it. If the first 14 characters substring of ln is master_console then increase variable n and if n not equals 2 then echo the line.
Else: echo the line.

Hope that helps to understand how it works.
For more information about the SET /P technique see:
viewtopic.php?f=3&t=2128

treefrog wrote:If there's a donation box please let me know where it is!

Haha, no I'm not here to earn money :) If you find that forum useful then you can help the owner keeping it online. There is a link on the bottom of the black box in the header of dostips.com.

Regards
aGerman

Post Reply