Wildcard failure

I am quite versed in writing Batch scripts for Windows, but even after all these years, how to properly escape from characters is puzzling. This is especially difficult when trying to figure out the correct way to avoid regex for use with sed. Is there any tool that can help me? Perhaps something that allows me to insert a β€œnormal” string and splash out a properly escaped version of this string?

Update: I do not want to give an example, because I am not looking for an answer on how to avoid a specific line. I am also not looking for a solution that will work for one specific application. I am looking for a tool that will help me get the escape syntax for every line I will ever need to avoid that the tool could use it from the command line.

With this regex that I really want

(^.*)(Form Product=")([^"]*") FormType="[^"]*" FormID="([0-9][0-9]*)".*$ 

Take this true regex (i.e. not bound to BATCH) and wrap it in some sed syntax like ssed "s@ --- Insert escaped regex here --- @http://psph/\1/\2@g" "%~1" , and finally open it ... Again, is there any tool that can help avoid any line to use on the BATCH command line?

ps There are so many exceptions to the BATCH syntax that I even agree to a good cheat sheet.

+38
windows scripting batch-file
Jul 26 '11 at 10:37
source share
4 answers

This is adapted with permission from the author of the Batch Files - Escape Symbols page on the scripting site of Rob van der Voude .

TLDR

Escaping batch file characters on Windows (and DOS) is difficult :

As in the universe, if someone ever fully understands the Package, then the language will be immediately replaced by an infinitely stranger and more complex version of itself. This obviously happened at least once before;)

Percent sign %

% can be escaped as %% - "It may not always be required [escaped] in double-quoted strings, just try"

Usually use a carriage ^

These characters "may not always be required to be [escaped] in double-quoted strings, but this will not hurt":

  • ^
  • &
  • <
  • >
  • |

Example: echo a ^> b to print a > b on the screen

' " is required [must be escaped] only in the FOR /F "subject" (that is, between parentheses) , if backq is not used

' "is required [must be escaped] only in the FOR /F " subject "(that is, between parentheses) , if backq used"

These characters are "required [for escaping] only in the FOR /F " subject "(that is, between parentheses), even in double-quoted strings":

  • ,
  • ;
  • =
  • (
  • )

Double Escape exclamation points when using a deferred variable extension

! must be escaped ^^! when pending variable expansion is activated.

Double double quotes in find search patterns

" β†’ ""

Use backslash in findstr regex patterns

  • \
  • [
  • ]
  • "
  • .
  • *
  • ?

Also

Rob commented on this question further (via email with me):

As for the answer, I'm afraid that the chaos is even worse than the original poster understands: the requirements for escaping brackets also depend on whether the line is inside the code block or not!

I believe that an automated tool can simply insert an insertion character in front of each character, and then double all the percent signs - and it still fails if the string is enclosed in double quotes!

In addition, individual programs are responsible for parsing their command line arguments, so some of the escaping is required, for example, for: for sed or ssed may be associated with specific programs invoked in batch scripts.

+49
Apr 15 '13 at 15:22
source share
β€” -

The equivalent symbol for a batch is a carriage ( ^ ). If you want to include any of the pipeline characters in your script, you need to prefix the character with a carriage:

 :: Won't work: @echo Syntax: MyCommand > [file] :: Will work: @echo Syntax: MyCommand ^> [file] 
+6
Jul 26 '11 at 13:37
source share

You can simply use an external file to enter sed.

Or using strings directly in batch mode, it is recommended that you use a delayed extension.

 setlocal DisableDelayedExpansion set "regEx=s/^#*$/""/g" setlocal EnableDelayedExpansion sed !regEx! file.txt 

EDIT: using unmodified package strings

This uses findstr to get the string directly from the package and return it to the result variable.
This way you can use the sed string as is.

 @echo off setlocal REM SedString1#(^.*)(Form Product=")([^"]*") FormType="[^"]*" FormID="([0-9][0-9]*)".*$ call :GetSEDString result SedString1 setLocal EnableDelayedExpansion echo the sedString is !result! sed !result! goto :eof :GetSEDString <resultVar> <searchName> :: Search the own batch file for <searchName> in a line with "REM <searchName>#" :: Return all after the "#" without any modification setLocal DisableDelayedExpansion for /f "usebackq tokens=* delims=" %%G in (`findstr /n /c:"REM %~2#" "%~f0"`) do ( set "str=%%G" ) setLocal EnableDelayedExpansion set "str=!str:*#=!" for /F "delims=" %%A in ("!str!") DO ( endlocal endlocal set "%~1=%%A" goto :eof ) goto :eof 
+3
Jul 26 '11 at 11:23
source share

An easy solution to save all command line arguments is to use %* : it returns the entire command line starting with the first command line argument (in Windows NT 4, %* also includes all leading spaces) and excluding any output redirection.

0
Feb 23 '18 at 9:33
source share



All Articles