Board logo

标题: [文本处理] 批处理删除txt文件中两特定字符之间的字符 [打印本页]

作者: weifanan    时间: 2013-7-3 08:50     标题: 批处理删除txt文件中两特定字符之间的字符

请教各位大神,如何用批处理命令删除两个特定字符串之间的字符?
比如a.txt中:
The great world serves us with food, life and everything.
we are going to shock the world with our virtue and wisdom!


如何删除with和and之间的字符,并同时删除and?

恳请各位不吝赐教!
作者: terse    时间: 2013-7-3 14:29

本帖最后由 terse 于 2013-7-3 14:45 编辑
  1. @echo off
  2. for /f "delims=" %%i in (a.txt) do (
  3. set str=%%i
  4.         setlocal enabledelayedexpansion
  5. if "!str: and =!" neq "%%i" if "!str: with =!" neq "%%i" (
  6. for %%j in (%%i) do if "!with!" == "" (
  7. set "s=!s! %%j"
  8. set %%j=w
  9. )
  10. set "str=!s:~1! !str:* and =!"
  11.         )
  12. echo !str!
  13.         endlocal
  14. )
  15. pause
复制代码

作者: weifanan    时间: 2013-7-3 14:49

感谢terse
试过,可以用。
如果我还想在删除with和and之间内容的同时删除and,该怎么改?
不好意思,我是菜鸟。希望terse能帮忙!
作者: terse    时间: 2013-7-3 14:57

提高点效率 但文件一行里含两个with 不好处理 文件里也不含有W才可以
  1. @echo off
  2. for /f "delims=" %%i in (a.txt) do (
  3. set str=%%i
  4. setlocal enabledelayedexpansion
  5. if "!str: and =!" neq "%%i" if "!str: with =!" neq "%%i" (
  6. set str=!str: with = withW!
  7. for /f "delims=W" %%j in ("!str!") do (
  8. set "str=%%j !str:* and =!"
  9. )
  10.         )
  11. echo !str!
  12. endlocal
  13. )
  14. pause
复制代码

作者: terse    时间: 2013-7-3 14:59

本帖最后由 terse 于 2013-7-3 15:01 编辑

回复 3# weifanan
and 不是删除了吗 你指的是那个AND、

处理后的 and 是删除的
The great world serves us with everything.
we are going to shock the world with wisdom!
作者: weifanan    时间: 2013-7-3 15:11

太感谢了!!!!




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2