I rent a piece of software which saves docs in a textfile.

Each question is formatted into clusters, with a label in square brackets, followed by a list of values associated to that question, following on separate lines, as per excerpt below:

[Q1]
Answer = yes
Source = A
Value = 20

[Q2]
Answer = yes
Source = B
Value = 20

[Q3]
Answer = yes
Source = A
Value = 20

What I would like to do is strip out any complete blocks of text where Source = B, so that the outputted text file of the example above would read as:

[Q1]
Answer = yes
Source = A
Value = 20

[Q3]
Answer = yes
Source = A
Value = 20

Basically omitting

[Q2]
Answer = yes
Source = B
Value = 20

Can this be done? if so, what is the best method

有帮助吗?

解决方案

The Batch file below was written precisely to solve this problem as it is stated, so it will not solve any other similar problem.

@echo off
setlocal EnableDelayedExpansion

set "Source = B"
for /F "delims=" %%a in (input.txt) do (
   set "line=%%a"
   if "!line:~0,1!" equ "[" (
      if "!Source !" neq " B" (
         echo !label!
         for %%v in (Answer Source Value) do echo %%v =!%%v !
         echo/
      )
      set "label=%%a"
   ) else (
      set "%%a"
   )
)
if "!Source !" neq " B" (
   echo !label!
   for %%v in (Answer Source Value) do echo %%v =!%%v !
   echo/
)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top