首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在文本文件中找到一个单词并编辑和删除下一行和单词

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

Stack Overflow用户
提问于 2015-09-17 11:33:56
回答 1查看 108关注 0票数 0

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

代码语言:javascript
复制
# 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

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

代码语言:javascript
复制
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

好的,举个例子,我想要找到新的A_C2_CC__Default_A___GLT_或者材料名称的一部分,然后我想告诉它将伊利2改为伊利4,材料名下的3行在这里是伊利姆,但是它可能会改变,所以我想告诉它,先找到材料名,然后找到第一个伊利,然后删除这一行,然后写伊利姆4。

我正在编写一个代码,但是我一天内什么都得不到,我会在这里之前发布一个代码,以获得您的支持。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-17 12:51:15

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

代码语言:javascript
复制
@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

编辑:添加使用示例

代码语言:javascript
复制
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:\>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32629333

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档