本帖最后由 pcl_test 于 2016-9-8 12:50 编辑
有下方类似样本文本,其中主体信息部分含大量=号,需要将其改变为其它字符以作后续处理:
list.txt- FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E5=A6=88
- FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E6=AF=9B=E5=93=A5
- FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E5=A6=B9=E5=A6=B9
- FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E4=BA=8C=E4=B8=87
- FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E6=96=B0=E4=B8=AD=E6=BA=90
- FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E5=B0=8F=E7=BF=A0
- FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E5=B0=BE=E5=B7=B4
- FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E7=88=B8=E7=88=B8
- FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E4=BC=8D=E5=B8=88=E5=82=85
- FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E5=B0=8F=E7=BD=97
- FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E7=89=A9=E7=AE=A1
- FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E5=AE=89=E8=A1=A3=E6=9F=9C=E9=97=A8=E5=B8=88=E5=82=85
- FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E5=86=92=E8=8F=9C
- FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E5=8C=85=E8=BA=AB=E5=B7=A5
- FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E5=BF=83=E5=AE=BD=E4=BD=93=E8=83=96
- FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E5=B0=8F=E9=B1=BC
- FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E8=B1=86=E6=B1=A4
- FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E5=B0=8F=E8=8D=89
- FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E4=B9=8C=E9=B8=A6
- FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E8=B0=A2=E5=A4=84
- FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E9=91=AB=E9=91=AB
- FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E8=90=A7=E8=90=A7=E9=BA=BB=E5=B0=86
- FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E5=A4=9C=E7=8C=AB
- FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E8=A5=BF=E6=9C=8D
- FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E6=B1=AA=E6=B1=AA
- FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E5=9B=9B=E4=B8=AA=E5=9C=88
- FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E7=A7=8B=E7=A7=8B
- FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E5=B0=8F=E7=BE=A9
- FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E9=99=88
- FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E9=9D=92=E8=9B=99
- FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=31=32=E6=A0=8B
复制代码 拿到题目后一看文本替换自然想到用set, 但具体实施的时候出问题了:SET中文本替换不能对=字符进行处理,试过很多办法,转义=号,转义"号都不起作用,正一筹莫展的时候突然想到论坛里面讨论过对于delims的默认取值及DOS默认分割符的相关讨论,=号正是经典分割符之一,比如echo,for 等均可以识别其为正确分割符,既然如此,就想到以下相对绕弯的方法来达到对set语句中=号替换的作用,如下方代码,就以%号来替换文本中的=号,核心语句恰好也是用的set本家的招式:- @echo off
- for /f "tokens=2 delims=:" %%i in (list.txt) do (
-
- for /f "usebackq tokens=*" %%k in (`"(for %%a in (%%i) do @<nul set /p=%%%%a)&echo,"`) do (
- set name=%%k
- call,echo,name = %%name%%
- )
- )
- pause
- goto :EOF
复制代码
|