搜索词在文件列表中,然后找到另一个替换为CLASS = ShipDummy整条生产线的那些话,取代它下面的2线 [英] Search for a list of words in a file, then find those words on another replacing the whole line with Class=ShipDummy,replacing the 2 lines below it

查看:161
本文介绍了搜索词在文件列表中,然后找到另一个替换为CLASS = ShipDummy整条生产线的那些话,取代它下面的2线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想(简单的一行字每一个),我们称之为LIST.TXT搜索的单词列表从外部列表,并在文件中搜索它们(C:\\用户\\ P小曲\\文档\\ SH3 \\ DATA \\ CFG \\ Backups_SCR * .clg)的(全字匹配,即使它在另一个和匹配的情况下)的,那么如果他们在那里找到另外的那些话文件(Campaign_SCR.mis.tmp)的(即使它在另一个匹配整个单词,匹配的情况下)的与名称更换整个行(Campaign_SCR.mis.tmp)= ShipDummy只有当行开始用NAME =。之后,在同一文件低于两行将与第2行类= ShipDummy,然后3号线改为类型= 206。

下面是code现在我有。现在,搜索类名= 变量在C:如果找到\\用户\\ P小曲\\文档\\ SH3 \\ DATA \\ CFG \\ Backups_SCR * .clg,然后将其替换类= ShipDummy整条生产线变量Campaign_SCR.mis.tmp。它也取代了它下面的1本着类型= 206。

  SETLOCAL enableDelayedExpansion> Campaign_SCR.mis.tmp(
FOR / F令牌= 1 * delims =%%的('FINDSTR / N^Campaign_SCR.mis.backup')做A(
    (!回声LN | FINDSTR^类型= $ 12> NUL&放大器;&放大器;设置LN = LN)|| (
        设置LN = %% B
        如果LN:〜!0,6!==级=(
            FINDSTR / S / C类名= LN:〜!6! C:\\用户\\ P小曲\\文档\\ SH3 \\ DATA \\ CFG \\ Backups_SCR \\ * CLG。>中C:\\用户\\ P小曲\\文档\\ SH3 \\ DATA \\ CFG \\ Backups_SCR \\空&放大器;&安培; (
                回声CLASS = ShipDummy
                设置LN =类型= 12
            )
        )
        !如果#LN ==#(回声)其他回声LN!
    )
  )

但我想它在我指定一个列表,搜索搜索的名称(C:\\用户\\ P小曲\\文档\\ SH3 \\ DATA \\ CFG \\ Backups_SCR * .clg)。然后如果发现匹配,然后搜索,在Campaign_SCR.mis.tmp并更换整条生产线,仅在该行以NAME =开头,以名称= ShipDummy。低于这两条线将与2号线CLASS = ShipDummy,然后第三行类型= 206取代:

 名称= ShipDummy
CLASS = ShipDummy
类型= 206

**请记住,在Campaign_SCR.mis.tmp变量的线不会经常出现名=变量。它很可能是名称=(字)变量(字)

可以在搜索列表是外部的?我们可以把它搜索每行一个单词。为〔实施例:如果列表如下:

俾斯麦
兜帽
浅水湾

它将搜索每个字**

当它从列表中的单词匹配(C:\\用户\\ P小曲\\文档\\ SH3 \\ DATA \\ CFG \\ Backups_SCR * .clg),请确保它匹配的情况下也是如此。因此,俾斯麦,从列表中会发现俾斯麦号从(C:\\用户\\ P小曲\\文档\\ SH3 \\ DATA \\ CFG \\ Backups_SCR * .clg),并没有发现俾斯麦

感谢您的时间!

顺便说一句,这可能是一个更简单的解决方法的问题,我这里:怎么办我将此?

因此,例如:

说我有一个外部列表(称之为LIST.TXT为例),如下所示:

 俾斯麦
兜帽
击退

我会搜索C:\\用户\\ P小曲\\文档\\ SH3 \\ DATA \\ CFG \\ Backups_SCR * .clg了点。说发现俾斯麦号和胡德在这:

  sadfasfasfdBismarckfasdfasdfasdfasasdfasfdafHoodasdfasfas

然后,它会搜索Campaign_SCR.mis.tmp俾斯麦和胡德替换:

 名称= asdfBismarckasfdw
CLASS = jlkjf
类型= 12

使用:

 名称= ShipDummy
CLASS = ShipDummy
类型= 206

 名称= asdfHoodasfdw
CLASS = jlkjf
类型= 13

使用:

 名称= ShipDummy
CLASS = ShipDummy
类型= 206


解决方案

 关闭@echo
SETLOCAL EnableDelayedExpansionREM INITIALIZE单词列表将搜索
设置targetWords =:EOFREM我想(简单的每个字上线)搜索来自外部列表的单词列表
FOR / F %%一中(LIST.TXT)做(
   REM和在文件中搜索它们(C:\\使用\\ P小曲\\文档\\ SH3 \\ DATA \\ CFG \\ Backups_SCR * .clg)
   FINDSTR%%一个C:\\使用\\ P小曲\\文档\\ SH3 \\ DATA \\ CFG \\ Backups_SCR * .clg> NUL
   REM,如果他们在那里?
   如果!错误级别! EQU 0(
      REM插入Word目标列表
      设置targetWords =!targetWords! %%一个
   )
)REM将档案结尾MARK在文件
回声:EOF>> Campaign_SCR.mis.tmpREM INITIALIZE最后处理线原则在中REDIRECTED Campaign_SCR.mis.tmp个数
设置lastLine所= 0REM ...找到另一个文件的那些话(Campaign_SCR.mis.tmp)
< Campaign_SCR.mis.tmp(FOR / Fdelims =%%一个在('FINDSTR / N%targetWords%Campaign_SCR.mis.tmp')做(
   REM DUPLICATE preVIOUS LINES直到有新的目标线
   集/ A numOfLines = %% A-lastLine所-1
   对/ L %%我在(1,1,!numOfLines!)做(
      集线=
      集/普线=
      回声(!行!
   )
   REM如果行开头NAME =
   集/普线=
   如果!行:0,5〜! EQUNAME =(
      REM更换整条生产线......与名称= ShipDummy
      回声名称= ShipDummy
      REM这两条线低于在同一文件将与第2行类= ShipDummy取代后,
      集/普线=
      回声CLASS = ShipDummy
      REM然后第三行类型= 206。
      集/普线=
      回声类型= 206
      集/ A lastLine所%% = I + 2
   )其他(
      REM复制非匹配行,如果不是:EOF MARK
      如果!行了! NEQ:EOF(
         回音!行!
         设置lastLine所= %%我
      )
   )
))GT; Campaign_SCR.mis.tmp.NEWREM更新新文件
REM德尔Campaign_SCR.mis.tmp
REM仁Campaign_SCR.mis.tmp.NEW Campaign_SCR.mis.tmp

I'd like to search for a list of words from an external list (simple each word on a line) which we'll call "List.txt", and search for them in a file (C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR*.clg) (matching the whole word, even if it's inside another, and matching case), then if they are there find those words on another file(Campaign_SCR.mis.tmp) (matching the whole word even if its inside another, matching case) replacing the whole line in (Campaign_SCR.mis.tmp) with Name=ShipDummy ONLY if the line starts with "Name=". After that the two lines below that in that same file would be replaced with 2nd line "Class=ShipDummy", then 3rd line "Type=206".

Here is the code I have now. Now it searches for ClassName=Variable in C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR*.clg, then it replaces the whole line with Class=ShipDummy if it finds the variable in Campaign_SCR.mis.tmp. It also replaces 1 line below it with "Type=206".

setlocal enableDelayedExpansion

>Campaign_SCR.mis.tmp (
for /f "tokens=1* delims=:" %%A in ('findstr /n "^" Campaign_SCR.mis.backup') do (
    ( echo !ln!| findstr "^Type=12$" >NUL && set ln=ln ) || (
        set "ln=%%B"
        if "!ln:~0,6!"=="Class=" (
            findstr /s /c:"ClassName=!ln:~6!" "C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR\*.clg" >"C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR\null" && (
                echo Class=ShipDummy
                set "ln=Type=12"
            )
        )
        if #!ln!==# (echo;) else echo !ln!
    )
  )
)

BUT I'd like it to search for a name from a list I specify, search in (C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR*.clg). Then if it finds the match, then search for that in Campaign_SCR.mis.tmp and replace the whole line, only if that line starts with "Name=", with Name=ShipDummy. The two lines below that would be replaced with 2nd line "Class=ShipDummy", then 3rd line "Type=206":

Name=ShipDummy
Class=ShipDummy
Type=206

**Please keep in mind that within Campaign_SCR.mis.tmp the variable's line wont often appear as Name=variable. It is likely to be Name=(words)variable(words)

Can the list that is searched be external? Can we make it search one word per line. For exmaple: if the list is as follows:

Bismarck Hood Repulse

It will search for each word.**

When it matches the words from the list to (C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR*.clg) please ensure that it matches case as well. So "Bismarck" from list will find "Bismarck" from (C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR*.clg), and not find "bismarck"

Thank you for your time!

By the way, this may be a simpler workaround to the problem I have here: How do I apply this?

So for example:

Say I have an external list (call it List.txt for example) that looks like this:

Bismarck
Hood
Repulse

I will search C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR*.clg for that. Say it finds "Bismarck" and "Hood" in this:

sadfasfasfdBismarckfasdfasdfasdfas

asdfasfdafHoodasdfasfas

Then it will search Campaign_SCR.mis.tmp for Bismarck and Hood replacing:

Name=asdfBismarckasfdw
Class=jlkjf
Type=12

With:

Name=ShipDummy
Class=ShipDummy
Type=206

And

Name=asdfHoodasfdw
Class=jlkjf
Type=13

With:

Name=ShipDummy
Class=ShipDummy
Type=206

解决方案

@echo off
setlocal EnableDelayedExpansion

REM INITIALIZE THE LIST OF WORDS THAT WILL BE SEARCHED
set targetWords=:EOF

rem I'd like to search for a list of words from an external list (simple each word on a line)
for /F %%a in (List.txt) do (   
   rem and search for them in a file (C:\Uses\P Ditty\Documents\SH3\data\cfg\Backups_SCR*.clg) 
   findstr "%%a" "C:\Uses\P Ditty\Documents\SH3\data\cfg\Backups_SCR*.clg" > NUL
   rem if they are there...
   if !errorlevel! equ 0 (
      REM INSERT THE WORD IN THE TARGET LIST
      set targetWords=!targetWords! %%a
   )
)

REM INSERT THE END-OF-FILE MARK IN THE FILE
echo :EOF>> Campaign_SCR.mis.tmp

REM INITIALIZE THE NUMBER OF LAST PROCESSED LINE IN REDIRECTED Campaign_SCR.mis.tmp
set lastLine=0

rem ... find those words on another file(Campaign_SCR.mis.tmp)
< Campaign_SCR.mis.tmp (for /F "delims=:" %%a in ('findstr /N "%targetWords%" Campaign_SCR.mis.tmp') do (
   REM DUPLICATE PREVIOUS LINES UNTIL NEW TARGET LINE
   set /A numOfLines=%%a-lastLine-1
   for /L %%i in (1,1,!numOfLines!) do (
      set line=
      set /P line=
      echo(!line!
   )
   rem if the line starts with "Name="
   set /P line=
   if "!line:~0,5!" equ "Name=" (
      rem replacing the whole line...with Name=ShipDummy
      echo Name=ShipDummy
      rem After that the two lines below that in that same file would be replaced with 2nd line "Class=ShipDummy", 
      set /P line=
      echo Class=ShipDummy
      rem then 3rd line "Type=206".
      set /P line=
      echo Type=206
      set /A lastLine=%%i+2
   ) else (
      REM DUPLICATE THE NON MATCHING LINE, IF IS NOT THE :EOF MARK
      if "!line!" neq ":EOF" (
         echo !line!
         set lastLine=%%i
      )
   )
)) > Campaign_SCR.mis.tmp.NEW

REM UPDATE THE NEW FILE
REM del Campaign_SCR.mis.tmp
REM ren Campaign_SCR.mis.tmp.NEW Campaign_SCR.mis.tmp

这篇关于搜索词在文件列表中,然后找到另一个替换为CLASS = ShipDummy整条生产线的那些话,取代它下面的2线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆