【问题标题】:How to find a word in a text file and edit and delete next lines and words如何在文本文件中查找单词并编辑和删除下一行和单词
【发布时间】:2015-12-14 06:20:06
【问题描述】:

我想使用批处理文件在文本文件中查找一个单词,然后我想删除包含该单词的行并删除下面的其他一些行,例如:

# Materials file generated by ZModeler 2.2.6 (Build 992).

newmtl Black__CarPaint_
Ka 1.00 1.00 1.00
Kd 1.00 1.00 1.00
Ks 0.15 0.15 0.15
illum 1

newmtl Carbon__CarPaint_
Ka 0.69 0.69 0.69
Kd 0.69 0.69 0.69
Ks 0.60 0.60 0.60
illum 1
map_Kd carbon.dds
refl cubeint.dds

newmtl Chrome__CarPaint_
Ka 1.00 1.00 1.00
Kd 1.00 1.00 1.00
Ks 1.00 1.00 1.00
illum 1
refl cubeint.dds

newmtl Details_Int__Default_A_
Ka 1.00 1.00 1.00
Kd 1.00 1.00 1.00
Ks 0.30 0.30 0.30
illum 1
map_Kd apollo_2008_details_int.dds
bump apollo_2008_details_int_ts.dds

newmtl Details_Int2__CarPaint_
Ka 1.00 1.00 1.00
Kd 1.00 1.00 1.00
Ks 0.40 0.40 0.40
illum 1
map_Kd apollo_2008_details_int2.dds
refl cubeint.dds

newmtl C1_Lea__Default_N_
Ka 0.95 0.46 0.07
Kd 0.95 0.46 0.07
Ks 0.10 0.10 0.10
illum 1
map_Kd moket2.dds
bump ts_leather3.dds

newmtl MAT_1__CarPaint_
Ka 0.69 0.69 0.69
Kd 0.69 0.69 0.69
Ks 0.60 0.60 0.60
illum 1
map_Kd carbon.dds
refl cubeint.dds

newmtl Moquette__Default_
Ka 0.36 0.36 0.36
Kd 0.36 0.36 0.36
Ks 0.20 0.20 0.20
illum 1
map_Kd moquette1.dds

newmtl Paint__CarPaint2_
Ka 1.00 1.00 1.00
Kd 1.00 1.00 1.00
Ks 1.00 1.00 1.00
illum 2
Ns 100.000000
bump fio_damage_2.dds
refl cubemap.dds
map_Kd user_tex.dds
map_Kd impact_maps.dds

newmtl Red__CarPaint_
Ka 1.00 1.00 1.00
Kd 1.00 1.00 1.00
Ks 0.20 0.20 0.20
illum 1

newmtl Screen__CarPaint_
Ka 1.00 1.00 1.00
Kd 1.00 1.00 1.00
Ks 0.20 0.20 0.20
illum 1
refl cubeint.dds

newmtl Plastic__CarPaint_
Ka 1.00 1.00 1.00
Kd 1.00 1.00 1.00
Ks 0.20 0.20 0.20
illum 1

newmtl Alcantara__CarVelour_
Ka 0.37 0.37 0.37
Kd 0.37 0.37 0.37
Ks 1.00 1.00 1.00
illum 2
Ns 100.000000
map_Kd moket2.dds
bump ts_moket2.dds
map_Ks moket2.dds

newmtl Details_Int_Alp__Default_A___GLT_
Ka 0.21 0.21 0.21
Kd 0.21 0.21 0.21
Ks 0.80 0.80 0.80
illum 2
Ns 80.000000
map_Kd apollo_2008_details_int.dds
bump apollo_2008_details_int_ts.dds
map_Ks apollo_2008_details_int.dds
refl cubeint.dds

newmtl Details_Int_Alp__Default_A___GLT_
Ka 0.23 0.23 0.23
Kd 0.23 0.23 0.23
Ks 0.20 0.20 0.20
illum 2
Ns 10.000000
map_Kd apollo_2008_details_int.dds
bump apollo_2008_details_int_ts.dds
map_Ks apollo_2008_details_int.dds

newmtl Details_Int2_Al__CarPaint_
Ka 0.95 0.95 0.95
Kd 0.95 0.95 0.95
Ks 0.10 0.10 0.10
illum 1
map_Kd apollo_2008_details_int2.dds

newmtl HUD__DashBoard_
Ka 1.00 1.00 1.00
Kd 1.00 1.00 1.00
Ks 0.30 0.30 0.30
illum 1
map_Kd apollo_2008_details_int.dds
refl cubeint.dds

newmtl Patch__Default_A_
Ka 0.44 0.44 0.44
Kd 0.44 0.44 0.44
Ks 0.00 0.00 0.00
illum 1
map_Kd apollo_2008_details_int2.dds
bump apollo_2008_details_int2_ts.dds

newmtl A_C2_C__Default_A___GLT_
Ka 0.89 0.53 0.15
Kd 0.89 0.53 0.15
Ks 0.10 0.10 0.10
illum 2
Ns 10.000000
map_Kd couture.dds
bump ts_couture.dds
map_Ks couture.dds

newmtl Leather__Default_N_
Ka 0.36 0.36 0.36
Kd 0.36 0.36 0.36
Ks 0.10 0.10 0.10
illum 1
map_Kd moket2.dds
bump ts_leather3.dds

newmtl HUD_01__DashBoard_
Ka 1.00 1.00 1.00
Kd 1.00 1.00 1.00
Ks 0.30 0.30 0.30
illum 1
map_Kd apollo_2008_details_int.dds
refl cubeint.dds

newmtl C3_L__Default_N_
Ka 1.00 0.60 0.00
Kd 1.00 0.60 0.00
Ks 0.10 0.10 0.10
illum 1
map_Kd moket2.dds
bump ts_leather3.dds

newmtl A_C1_P__Default_A_
Ka 1.00 0.51 0.18
Kd 1.00 0.51 0.18
Ks 0.10 0.10 0.10
illum 1
map_Kd apollo_2008_details_int2.dds
bump apollo_2008_details_int2_ts.dds

newmtl Belt__Default_
Ka 1.00 1.00 1.00
Kd 1.00 1.00 1.00
Ks 0.20 0.20 0.20
illum 2
Ns 10.000000
map_Kd seatbelt.dds
map_Ks seatbelt.dds

newmtl Grip__Default_N_
Ka 0.35 0.35 0.35
Kd 0.35 0.35 0.35
Ks 0.20 0.20 0.20
illum 2
Ns 10.000000
map_Ks dot2.dds
bump ts_dot2.dds

newmtl Plastic2__CarPaint_
Ka 1.00 1.00 1.00
Kd 1.00 1.00 1.00
Ks 0.20 0.20 0.20
illum 1

newmtl A_C2_CC__Default_A___GLT_
Ka 0.89 0.53 0.15
Kd 0.89 0.53 0.15
Ks 0.10 0.10 0.10
illum 2
Ns 10.000000
map_Kd couture_croix.dds
bump couture_croix_ts.dds
map_Ks couture_croix.dds

newmtl C3_Pla__Default_
Ka 0.29 0.29 0.29
Kd 0.29 0.29 0.29
Ks 0.20 0.20 0.20
illum 1

newmtl Glass_I__CarGlassDirt_
Ka 1.00 1.00 1.00
Kd 1.00 1.00 1.00
Ks 0.00 0.00 0.00
illum 4
map_Kd apollo_2008_glass_i.dds

如您所见,每个短语的格式都与其他短语相同

newmtl A_C2_CC__Default_A___GLT_  :The material name
Ka 0.89 0.53 0.15   :Some of the numbers for transparency reflection and ...
Kd 0.89 0.53 0.15   :Some of the numbers for transparency reflection and ...
Ks 0.10 0.10 0.10   :Some of the numbers for transparency reflection and ...
illum 2             :One illum
Ns 10.000000        :Some of the numbers for transparency reflection and ...
map_Kd couture_croix.dds :Texture names
bump couture_croix_ts.dds :Texture names
map_Ks couture_croix.dds  :Texture names

好的,例如,我想找到 newmtl A_C2_CC__Default_A___GLT_ 或材料名称的一部分,然后我想告诉它将 illum 2 更改为 illum 4,材料名称下的 3 行是 illum,但它可能会改变,所以我想要告诉它首先找到材料名称,然后找到第一个 illum,然后删除该行并改写 illum 4。

我正在编写代码,但一天之内我什么也得不到

【问题讨论】:

    标签: batch-file replace find text-files


    【解决方案1】:

    我建议您将此批处理文件命名为 change.bat

    @echo off
    setlocal EnableDelayedExpansion
    
    if "%~4" neq "" goto begin
    echo Usage: %0 item=value IN materialName
    echo/
    echo For example:
    echo    %0 illum=4 IN A_C2_CC__Default_A___GLT_
    goto :EOF
    
    :begin
    
    set "var=%~1"
    set "value=%~2"
    
    rem Get the number of the line that contain the materialName (the first instance only)
    for /F "delims=:" %%a in ('findstr /N /C:"%~4" input.txt') do set "lines=%%a" & goto continue
    :continue
    
    rem Open a code block to read from input.txt and write to output.txt
    < input.txt (
    
       rem Read and write the first "lines" lines
       for /L %%i in (1,1,%lines%) do (
          set "line="
          set /P "line="
          echo(!line!
       )
    
       rem Search for the desired variable and change its value
       call :changeVar
    
       rem Copy the rest of lines
       findstr "^"
    
    ) > output.txt
    
    rem Replace the original file by the new one
    move /Y output.txt input.txt >NUL
    goto :EOF
    
    
    :changeVar
       set "line="
       set /P "line="
       for /F %%a in ("%line%") do (
          if "%%a" equ "%var%" (
             echo %%a %value%
             goto endChange
          )
       )
       echo(%line%
    goto changeVar
    :endChange
    exit /B
    

    编辑添加了使用示例

    C:\> copy input.txt inputOrig.txt
            1 archivo(s) copiado(s).
    
    C:\> change
    Usage: change item=value IN materialName
    
    For example:
       change illum=4 IN A_C2_CC__Default_A___GLT_
    
    C:\> change illum=4 IN A_C2_CC__Default_A___GLT_
    
    C:\> fc inputOrig.txt input.txt
    Comparando archivos inputOrig.txt y INPUT.TXT
    ***** inputOrig.txt
    Ks 0.10 0.10 0.10
    illum 2
    Ns 10.000000
    ***** INPUT.TXT
    Ks 0.10 0.10 0.10
    illum 4
    Ns 10.000000
    *****
    
    
    C:\>
    

    【讨论】:

    • 非常感谢,但我无法使用它,您能解释一下如何使用它吗? tnx 你在这方面真的很棒。 :)
    • 谢谢它现在可以与 illums 一起使用 :) 但它不适用于 Kd 0.29 0.29 0.29 您可以将 Kd 0.29 更改为任何值,但我无法更改接下来的两个 0.29 和 0.29 数字。
    • 只需将整个值括在引号中:change Kd="0.29 0.29 0.29" IN A_C2_CC__Default_A___GLT_
    猜你喜欢
    • 1970-01-01
    • 2020-02-01
    • 1970-01-01
    • 2012-10-31
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    • 2019-03-12
    • 2013-03-29
    相关资源
    最近更新 更多