Displaying the “libraried” instances of prediefined strings within a file in CMD

半城伤御伤魂 提交于 2020-01-06 06:37:08

问题


All commands must be performed in Windows Command Prompt

I have a file data.txt which has several strings in it:

gargonxx**stringX**moregargon
gargonargongargongargon
gargon**stringZ**xxgargonxxxx

and for this data file, I want to create a "library" file:

stringX = informationx
stringY = informationy
stringZ = informationz

then create variables in CMD out of the "information" shown in the "library" file, ONLY of the instances in the data.txt file that match with the library file.

varx = string's informationx

and then relay this information in the command window.

echo varx

How do I go about

  1. Having CMD recognize the instances that match
  2. Relaying the instance's information in the command window

Here are my LIBRARY and DATA files


回答1:


@ECHO OFF
SETLOCAL
FOR /f "delims==" %%a IN ('set $ 2^>nul') DO "SET %%a="
FOR /f "delims=" %%a IN (data.txt) DO (
 FOR /f "tokens=1*delims== " %%L IN (library.txt) DO (
  IF /i "%%a"=="%%L" SET $%%a=%%M&ECHO(%%M
 )
)
ECHO ==== OR =====
SET $

GOTO :EOF

use a text editor to create the library file.

This batch matches the instances that match.

Showing the information depends on what you want to do.

With 'data.txt`

lineX
lineZ

and library.txt

lineX = informationx
lineY = informationy
lineZ = informationz

the above batch showed

informationx
informationz
==== OR =====
$lineX=informationx
$lineZ=informationz

For revised specification

@ECHO OFF
SETLOCAL
FOR /f "delims==" %%a IN ('set $ 2^>nul') DO "SET %%a="
FOR /f "tokens=1*delims== " %%L IN (library.txt) DO (
 FINDSTR /i /L "%%L" data.txt >nul
 IF NOT ERRORLEVEL 1 SET $%%L=%%M&ECHO(%%M
)
ECHO ==== OR =====
SET $

GOTO :EOF

(same results, except line replaced by string)



来源:https://stackoverflow.com/questions/20966845/displaying-the-libraried-instances-of-prediefined-strings-within-a-file-in-cmd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!