标题: [文本处理] 批处理删除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 编辑
- @echo off
- for /f "delims=" %%i in (a.txt) do (
- set str=%%i
- setlocal enabledelayedexpansion
- if "!str: and =!" neq "%%i" if "!str: with =!" neq "%%i" (
- for %%j in (%%i) do if "!with!" == "" (
- set "s=!s! %%j"
- set %%j=w
- )
- set "str=!s:~1! !str:* and =!"
- )
- echo !str!
- endlocal
- )
- pause
复制代码
作者: weifanan 时间: 2013-7-3 14:49
感谢terse
试过,可以用。
如果我还想在删除with和and之间内容的同时删除and,该怎么改?
不好意思,我是菜鸟。希望terse能帮忙!
作者: terse 时间: 2013-7-3 14:57
提高点效率 但文件一行里含两个with 不好处理 文件里也不含有W才可以- @echo off
- for /f "delims=" %%i in (a.txt) do (
- set str=%%i
- setlocal enabledelayedexpansion
- if "!str: and =!" neq "%%i" if "!str: with =!" neq "%%i" (
- set str=!str: with = withW!
- for /f "delims=W" %%j in ("!str!") do (
- set "str=%%j !str:* and =!"
- )
- )
- echo !str!
- endlocal
- )
- 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 |