如何在文本文件中查找单词并编辑和删除下一行和单词

时间:2015-09-17 11:33:57

标签: batch-file replace find text-files

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

# 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 here但是它可能会改变所以我想要告诉它首先找到材料名称然后找到第一个illum然后删除该行并写入illum 4。

我正在编写一个代码但是我在一天内无法获得任何内容我会发布代码直到这里tnx以获得您的支持。

1 个答案:

答案 0 :(得分: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:\>