使用Windows批处理脚本仅提取特定字符串

时间:2014-01-24 07:37:55

标签: batch-file batch-processing

使用Windows Batch脚本,我需要输出如下所示。请帮忙。

输入:

APP "Default Web Site/" (applicationPool:DefaultAppPool)  
APP "Default Web Site/App1" (applicationPool:App1)  
APP "Default Web Site/App1/Pages" (applicationPool:App1)  
App "Site1/App2/" (applicationPool:App2)  
App "Site1/App3/VD1" (applicationPool:VD1)

输出:

Default Web Site  
Site1

4 个答案:

答案 0 :(得分:0)

@echo off
setlocal
set "arg_two=%~2"
for /f "tokens=1 delims=/" %%a in  ("%arg_two%") do echo %%a
endlocal

试验:

outputparser.bat APP "Default Web Site/" (applicationPool:DefaultAppPool)
>Default Web Site
outputparser.bat App "Site1/App3/VD1" (applicationPool:VD1)
>Site1

但是如果你在输入周围设置引号就行不了。

带有待处理文件的

编辑(内容与问题中的内容相同):

@echo off
setlocal
set "file_to_process=.\input_file.txt"
for /f "usebackq tokens=1 delims=(" %%W in ("%file_to_process%") do (
    for /f usebackq^ tokens^=2^ delims^=/^"  %%a in  ('%%~W') do (
         echo %%a
    )
)
endlocal

答案 1 :(得分:0)

这适用于样本数据。

@echo off
(for /f "delims=/" %%a in (file.txt) do (
   for /f "tokens=1,*" %%b in ("%%~a") do (
      echo %%~c
   )
))>temp.fil

sort <temp.fil |uniq 
del temp.fil
pause

以下代码为uniq.bat
将uniq.bat放在与上面的批处理文件相同的文件夹中或放在路径上的目录中。

@if (@CodeSection == @Batch) @then

@CScript //nologo //E:JScript "%~F0" & goto :EOF & Rem aacini 2013

@end

var line, prevLine = "";
while ( ! WScript.Stdin.AtEndOfStream ) {
   line = WScript.Stdin.ReadLine();
   if ( line != prevLine ) {
      WScript.Stdout.WriteLine(line);
      prevLine = line;
   }
}

答案 2 :(得分:0)

@echo off
    setlocal enableextensions enabledelayedexpansion

    set "inputFile=c:\somewhere\file.dat"

    rem Extract the required part of the lines
    set "tempFile=%temp%\%~nx0.%random%.tmp"
    > "%tempFile%" (
        for /f usebackq^ tokens^=2^ delims^=^/^" %%a in ("file.txt") do (echo(%%a)
    )

    rem Remove the duplicates from the list
    set "previous="
    for /f "tokens=*" %%f in ('type "%tempFile%" ^| sort') do (
        if not "%%f"=="!previous!" (
            echo(%%f
            set "previous=%%f"
        )
    )

    rem Cleanup
    del "%tempFile%" >nul 2>nul
    endlocal

它使用引号和斜杠作为分隔符来剪切所需点上的线条。然后对列表进行排序以消除输出中的重复

答案 3 :(得分:0)

@echo off
setlocal enableDelayedExpansion
set "val="
for /f tokens^=2^ delims^=/^" %%A in ('sort input.txt') do (
  if "!val!" neq "%%A" echo %%A
  set "val=%%A"
)

如果某个值可能包含!,则需要

@echo off
setlocal disableDelayedExpansion
set "val="
for /f tokens^=2^ delims^=/^" %%A in ('sort input.txt') do (
  set "new=%%A"
  setlocal enableDelayedExpansion
  if "!val!" neq "!new!" echo !new!
  endlocal
  set "val=%%A"
)