[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[文本处理] 批处理删除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: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
复制代码

TOP

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

TOP

提高点效率 但文件一行里含两个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
复制代码

TOP

本帖最后由 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!

TOP

太感谢了!!!!

TOP

返回列表