标题: [文本处理] 求助,批处理如何替换每行的多个字符串? [打印本页]
作者: pk987 时间: 2012-12-31 16:40 标题: 求助,批处理如何替换每行的多个字符串?
我下载了一些网页文件,findstr 可售 bb.txt >1.txt 提取了包含 可售的行,并保存到1.txt,内容如下(行前有空格):
<td id="saleinfo" align="center">总售:199套 可售:199套</td>
<td id="saleinfo" align="center">总售:347套 可售:263套</td>
<td id="saleinfo" align="center">总售:580套 可售:570套</td>
<td id="saleinfo" align="center">总售:612套 可售:589套</td>
<td id="saleinfo" align="center">总售:648套 可售:585套</td>
我想将每行,以这样的方式输出
总售;199;可售;199
总售;347;可售;263
总售;580;可售;570
总售;612;可售;589
总售;648;可售;585
--我在这里学了一下,可以用下面的命令达到效果:
copy build*.pub bb.txt
for /f "tokens=2 delims=>" %%a in ('findstr /i /c:"可售" bb.txt') do echo=%%a >>b1.txt
for /f "tokens=1 delims=</td" %%b in (b1.txt) do echo=%%b >>b2.txt
for /f "tokens=1-2 delims=套 " %%c in (b2.txt) do echo=%%c;%%d>>b3.txt
for /f "tokens=1-3 delims=:" %%e in (b3.txt) do echo=%%e;%%f;%%g>>b4.txt
echo=总售;=sum(b1:b1579);可售;=sum(d1:d1579)>>b4.txt
del b1.txt =总售:199套 可售:199套</td
del b2.txt=总售:199套 可售:199套
del b3.txt=总售:199;可售:199
b4.txt=总售;199;可售;199
-------上面的方法,都是以关键字,将每行分割成多列,之后提取特定的列,但delims=并不能很好支持长字符串,如下面的第一个。
我想以变量的方式,替换每行的字符口串为“;”
“ <td id="saleinfo" align="center">”
“:”
“套”
“ ”
“</td>”
作者: pk987 时间: 2012-12-31 16:52
我不想用第三方命令,用下面的方法,能删除变量的前后部分,但对中间的无效,它也不能替换为;号
setlocal enabledelayedexpansion
for /f "tokens=*" %%i in (1.txt) do (
set str=%%i
set str=!str:~33,-5!
echo=!str!>>b3.txt
)
作者: apang 时间: 2012-12-31 17:08
- @echo off
- for /f "tokens=3,4,6,7 delims=<>&;:套" %%a in (1.txt) do (
- echo,%%a;%%b;%%c;%%d
- )
- pause
复制代码
作者: pk987 时间: 2012-12-31 17:28
多谢,不过这还是分割取列的方法。就不能变量替换吗?
作者: BAT-VBS 时间: 2012-12-31 18:09
- @echo off
- setlocal enabledelayedexpansion
- >b3.txt type nul
- for /f "tokens=*" %%i in (a.txt) do (
- set str=%%i
- set str=!str:~33,-5!
- set "str=!str: =!"
- set str=!str::=;!
- >>b3.txt echo,!str!
- )
复制代码
作者: pk987 时间: 2012-12-31 18:34
5楼的方法,测试可行,先用变量去头尾,解决最麻烦的“ <td id="saleinfo" align="center">”,之后就方便处理了,多谢了。
只是这第一段,就真不能替换处理吗?
作者: BAT-VBS 时间: 2012-12-31 18:52
回复 6# pk987 - @echo off
- setlocal enabledelayedexpansion
- >b3.txt type nul
- for /f "tokens=*" %%i in (a.txt) do (
- set str=%%i
- set "str=!str:*总售:=总售;!"
- set "str=!str:</td>=!"
- set "str=!str: =!"
- set str=!str::=;!
- >>b3.txt echo,!str!
- )
复制代码
作者: pk987 时间: 2012-12-31 19:04
经过测试,如果不用 set str=!str:~33,-5!
改用如下代码,可以得到
==;总售;199;可售;199;
==;总售;347;可售;263;
==;总售;580;可售;570;
==;总售;612;可售;589;
这效果,也够用了。
代码
@echo off
del a2.txt
setlocal enabledelayedexpansion
>a2.txt type nul
for /f "tokens=*" %%i in (1.txt) do (
set str=%%i
set "str=!str: =!"
set str=!str::=;!
set str=!str:套=;!
set str=!str:td id=!
set str=!str:"saleinfo" align=!
set str=!str:"center"=!
set str=!str:/td=!
set "str=!str:<>=!"
set "str=!str:<=!"
set "str=!str:>=;!"
>>a2.txt echo,!str!
)
call exit
set str=!str:~33,-5!
---谢谢各位的帮助
作者: pk987 时间: 2012-12-31 19:21
本帖最后由 pk987 于 2012-12-31 21:30 编辑
copy build*.pub bb.txt
@echo off
setlocal enabledelayedexpansion
>a0.txt type nul
for /f "tokens=*" %%i in ('findstr /i /c:"可售" bb.txt') do (
set str=%%i
set "str=!str:*总售:=总售;!"
set "str=!str:套</td>=!"
set "str=!str:套 =!"
set str=!str::=;!
>>a0.txt echo,!str!
)
`call exit
@echo off
>a1.txt type nul
echo=总售;=sum(b2:b3000);可售;=sum(d2:d3000)>>a1.txt
for /f "tokens=3,4,6,7 delims=<>&;:套" %%a in ('findstr /i /c:"可售" bb.txt') do (echo,%%a;%%b;%%c;%%d>>a1.txt)
`call exit
@echo off
setlocal enabledelayedexpansion
>a2.txt type nul
echo=ab;总售;=sum(c2:c3000);可售;=sum(e2:e3000)>>a2.txt
for /f "tokens=*" %%i in ('findstr /i /c:"可售" bb.txt') do (
set str=%%i
set "str=!str: =!"
set str=!str::=;!
set str=!str:套=;!
set str=!str:td id=!
set str=!str:"saleinfo" align=!
set str=!str:"center"=!
set str=!str:/td=!
set "str=!str:<>=!"
set "str=!str:<=!"
set "str=!str:>=;!"
>>a2.txt echo,!str!
)
`call exit
@echo off
setlocal enabledelayedexpansion
>a3.txt type nul
echo=总售;=sum(b2:b3000);可售;=sum(d2:d3000)>>a3.txt
for /f "tokens=*" %%i in ('findstr /i /c:"可售" bb.txt') do (
set str=%%i
set str=!str:~33,-5!
set "str=!str: =!"
set str=!str::=;!
set str=!str:套=!
>>a3.txt echo,!str!
)
`call exit
@echo off
>a4.txt type nul
for /f "tokens=2 delims=>" %%a in ('findstr /i /c:"可售" bb.txt') do echo=%%a >>b1.txt
for /f "tokens=1 delims=</td" %%b in (b1.txt) do echo=%%b >>b2.txt
for /f "tokens=1-2 delims=套 " %%c in (b2.txt) do echo=%%c;%%d>>b3.txt
echo=总售;=sum(b2:b3000);可售;=sum(d2:d3000)>>a4.txt
for /f "tokens=1-3 delims=:" %%e in (b3.txt) do echo=%%e;%%f;%%g>>a4.txt
del b1.txt
del b2.txt
del b3.txt
call exit
call exit
copy build*.pub bb.txt
findstr /i /c:"可售" bb.txt >1.txt
五个方法全贴吧,也给自己留个备份。
作者: BAT-VBS 时间: 2012-12-31 19:44
回复 8# pk987
7楼的不行吗?
作者: pk987 时间: 2012-12-31 21:32
不好意思,我也在看贴,没留意七楼,七楼的方法我试了,可行,非常感谢你的帮忙。
作者: guaiwu 时间: 2012-12-31 22:13
本帖最后由 guaiwu 于 2012-12-31 23:03 编辑
- @echo off
-
- for /f "tokens=2,4 delims=>&;<" %%a in (1.txt) do (
- echo %%a %%c
- )
- pause
- )
- pause
复制代码
借取三楼的代码的。
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |