Jump to content

  • Log In with Google      Sign In   
  • Create Account

Remove Multi-Line String from File


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 gretty   Members   -  Reputation: 212

Like
0Likes
Like

Posted 30 April 2013 - 05:07 PM

Hello

I am attempting to remove a multiline string from a file. The multiline string comes from another file and is read into a Batch variable. I then read the target file and search for this multiline string, if it exists I want to delete it from the target file.

Is this kind of functionality possible using Batch Windows Scripting? I really hope it is.

My code below is unable to remove the multiline string from the target file. It can successfully read the input file. Can you help me edit my script to search for and remove a multiline string from a file?

 

@echo off &setlocal enabledelayedexpansion

Set replace=
Set target=
Set infile=usermenuTest1.4d
Set outfile=usermenuTest2.4d

Rem Read file and store all contents in string
for /f "delims=" %%i in (%infile%) do set "target=!target! %%i"
echo %target%

Rem Remove the target string from outfile
@echo off & setlocal enabledelayedexpansion
for /f "tokens=1,* delims=¶" %%A in ( 'type "%outfile%"') do SET "string=%%A"
SET "modified=!string:%target%=%replace%!"
(echo(%modified%)>> "%outfile%"

ECHO.
PAUSE
ENDLOCAL

 

Essentially I want my script to remove the following bold text from a file:
 

// abc

// def

// hij

Menu "User" {
   Button "" {
      Walk_Right ""
   }
}

 



Sponsor:

#2 ApochPiQ   Moderators   -  Reputation: 15833

Like
1Likes
Like

Posted 01 May 2013 - 03:32 PM

Is it absolutely mandatory to do this using batch? It's really not designed for this sort of thing and I imagine that (if it's possible at all) the solution will be hideous.

Why not use a language like Python or even PowerShell instead?

#3 frob   Moderators   -  Reputation: 21493

Like
0Likes
Like

Posted 01 May 2013 - 04:11 PM

You can use awk / gawk to do it pretty simply, but this is Windows.

 

There is a port of many useful Unix utilities here.  

 

Awk is a utility to scan files for patterns and then do something with the matching lines.

 

There is a command line switch to output everything that does not match.  You want to output all the lines that don't match your pattern, and write that to a file.


Check out my personal indie blog at bryanwagstaff.com.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS