Board logo

标题: [求助]难题,再更新,还继续求助中:如何替换~|为☆ [打印本页]

作者: cny798     时间: 2010-7-23 01:39    标题: [求助]难题,再更新,还继续求助中:如何替换~|为☆

请问,如何用批处理的命令把1.txt里的~|替换为☆?(不能用其它软件)谢谢帮助的朋友(小提醒,1、“~|”两个字符都是特殊转义字符,需要留意;2、要留意换行等格式不能变;3、实际需求中,文件内容大约是下面文件的五万倍以上,每天都需要做此替换工作,所以也请考虑一下程序的运行效率问题,谢谢谢谢。----2010.07.24 15:32)


1.txt的内容
Country~|Case No~|Journal ID~|Case Login Name~|Case Creation Date/Time~|Case Created By Dept~|Journal Creation Date/Time~|Journal Created By Dept~|Journal Login Name~|Log Status~|Service Request No~|Current Status~|Service Code~|Resolution Code~|Component Code~|Cause Code~|
Reason Code~|Case Summary~|Service Tag~|LOB~|Item Family~|Customer Code~|Email~|Support


2.txt的内容
Country☆Case No☆Journal ID☆Case Login Name☆Case Creation Date/Time☆Case Created By Dept☆Journal Creation Date/Time☆Journal Created By Dept☆Journal Login Name☆Log Status☆Service Request No☆Current Status☆Service Code☆Resolution Code☆Component Code☆Cause Code☆
Reason Code☆Case Summary☆Service Tag☆LOB☆Item Family☆Customer Code☆Email☆Support


案例:
错误的程序

@echo off
setlocal disabledelayedexpansion
set oldfile="d:\1.txt"
set newfile="d:\2.txt"

if exist d:\2.txt del d:\2.txt

(for /f "tokens=*" %%a in ('findstr /n .* %oldfile%') do (
  set var=%%a
  setlocal enabledelayedexpansion
  if "!var:*:=!"=="" (echo;) else (
    set var=!var:*:=!
    echo;!var:^~^|=☆!
  )
  endlocal
))>%newfile%

[ Last edited by cny798 on 2010-7-24 at 16:04 ]
作者: Taurus     时间: 2010-7-23 03:38
@echo off
for /f "delims=" %%i in ('more 1.txt') do (
    set str=%%i
    call set str=%%str:^|=☆%%
    call echo %%str%%
)
pause
作者: cny798     时间: 2010-7-23 12:53


  Quote:
Originally posted by Taurus at 2010-7-23 03:38:
@echo off
for /f "delims=" %%i in ('more 1.txt') do (
    set str=%%i
    call set str=%%str:^|=☆%%
    call echo %%str%%
)
pause

非常感谢您的回复,但这是错误的,因为~与|都是特殊字符,这样是不行的。
作者: WANKOILZ     时间: 2010-7-23 14:30
我来钻个空子先(不要拿鸡蛋砸我啊...):
@echo off
(for /f "delims=" %%i in ('findstr /n .* 1.txt') do (
set "str=%%i"
setlocal enabledelayedexpansion
if "!str:*:=!"=="" (echo;) else (
       set str=!str:*:=!
       echo;!str:^|=☆!
)))
echo 请复制粘贴!&pause>nul

作者: defanive     时间: 2010-7-23 15:17
想不出什么办法了,只好枚举字串符
@echo off
setlocal enabledelayedexpansion
set/p str=<1.txt
set ptr=0,pre=0
:loop
if "!str:~%ptr%,2!"=="~|" (
        set /a lng=ptr-pre
        call set new=!new!%%str:~!pre!,!lng!%%☆
        set /a pre=ptr+2
)
if "!str:~%ptr%,2!"=="" (
        set new=!new!!str:~%pre%!
        echo !new!>2.txt
        exit
)
set /a ptr+=1
goto loop

作者: cny798     时间: 2010-7-23 21:10


  Quote:
Originally posted by defanive at 2010-7-23 15:17:
想不出什么办法了,只好枚举字串符
[code]@echo off
setlocal enabledelayedexpansion
set/p str=<1.txt
set ptr=0,pre=0
:loop
if "!str:~%ptr%,2!"=="~|" (
        se ...

首先,非常感谢这位朋友的回复。
其次,这段代码还是有点问题,遇到1.txt的内容出现换行时,它只会“翻译”第一段,就停止了。(即:遇到换行之后的内容,不会继续运行)

作者: defanive     时间: 2010-7-23 21:25
大可自己改下。。。
我读取只读了第一行,你读多几行就是了。。。
作者: netbenton     时间: 2010-7-24 08:14

@echo off&setlocal enabledelayedexpansion
set oldfile="d:\1.txt"
set newfile="d:\2.txt"

if exist d:\2.txt del d:\2.txt

(for /f "tokens=*" %%a in ('findstr /n .* %oldfile%') do (
  set var=%%a
  set var=!var:*:=!
  set str=0
  for /l %%a in (1,1,30) do (
    rem 用for /f 来分段处理,当前可以处理30X2段,如果更多你可以把30改大一点,不过这个值越大速度越慢
     for /f "tokens=1,2* delims=~|" %%b in ("!var!") do (
        if "%%b" neq "" (set str=!str!☆%%a☆%%b) else (set str=!str!☆%%a)
        set var=%%c
     )
  )
  echo;!str:~2!
))>%newfile%
endlocal
[ Last edited by netbenton on 2010-7-23 at 23:36 ]
作者: freeants001     时间: 2010-7-24 11:11
保存下面代码为doit.bat,命令提示符下输入doit   d:\1.txt试试:
@echo off&setlocal ENABLEDELAYEDEXPANSION
cd/d "%~dp0">nul
set/a D_NUM=%~z1+255
call:DEC_HEX %D_NUM% >nul
for /f "eol=- skip=1 tokens=*" %%i in ('^(echo d100,%FILE_SIZE%^&echo q^)^|debug %~s1') do (
    set op=%%i
    set op=!op:-= !
    set op=!op:7E=A1!
    set op=!op:7C=EE!
    echo e!op:~5,53! 2>nul >>"%~n1.$"
)
echo rcx>>"%~n1.$"
call:DEC_HEX %~z1 >nul
echo %FILE_SIZE%>>"%~n1.$"
echo n$tmp$>>"%~n1.$"
echo w>>"%~n1.$"
echo q>>"%~n1.$"
debug<"%~n1.$"
ren $tmp$ dstfile.txt
del/a/q $tmp$ "%~n1.$"
start notepad dstfile.txt
goto :EOF

:DEC_HEX
    set FILE_SIZE=
    set PATTERN=123456789ABCDEF
    set dec=%1
    :WHILE
    set /a res=%dec%%%16-1
    set /a dec/=16
    set res=!PATTERN:~%res%,1!
    set FILE_SIZE=%res%%FILE_SIZE%
    if "%dec%" neq "0" goto WHILE
goto:eof
[ Last edited by freeants001 on 2010-7-24 at 12:46 ]
作者: cny798     时间: 2010-7-24 14:28


  Quote:
Originally posted by netbenton at 2010-7-24 08:14:
[code]@echo off&setlocal disabledelayedexpansion
set oldfile="d:\1.txt"
set newfile="d:\2.txt"

if exist d:\2.txt del d:\2.txt

(for /f "tokens=*" %%a in ('f ...

这位朋友,首先,感谢您的回复;其次,您的程序是错误的,您可以测试一下,运行结果如下:

!str:~2!
!str:~2!
!str:~2!
!str:~2!
!str:~2!
!str:~2!
!str:~2!
!str:~2!
作者: defanive     时间: 2010-7-24 14:53

@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in (1.txt) do (
        set "str=%%a"
        call :sub
)
goto :eof
:sub
set ptr=0
set pre=0
set "new="
:loop
if "!str:~%ptr%,2!"=="~|" (
        set /a lng=ptr-pre
        call set new=!new!%%str:~!pre!,!lng!%%☆
        set /a pre=ptr+2
)
if "!str:~%ptr%,2!"=="" (
        set new=!new!!str:~%pre%!
        echo !new!>>2.txt
        goto :eof
)
set /a ptr+=1
goto loop
。。。
作者: cny798     时间: 2010-7-24 15:33


  Quote:
Originally posted by freeants001 at 2010-7-24 11:11:
保存下面代码为doit.bat,命令提示符下输入doit   d:\1.txt试试:

[code]
@echo off&setlocal ENABLEDELAYEDEXPANSION
cd/d "%~dp0">nul
set/a D_NUM=%~z1+255 ...

谢谢这位朋友的回复,但这段代码调用到debug.exe了,在Win7里不兼容,同时,由于条件限制,也不能调用debug.exe,期待有更理想的作品,谢谢。
作者: cny798     时间: 2010-7-24 15:40


  Quote:
Originally posted by defanive at 2010-7-24 14:53:
[code]@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in (1.txt) do (
        set "str=%%a"
        call :sub
)
goto :eof
:sub
set ptr=0
set pre=0
set "new=" ...

非常感谢这位朋友的回复,非常感谢您的作品,这是目前为止,唯一正确的。
如果运行速度可以改善则更佳了,因为实际使用中,需要转换的文件的内容大约是例中的五万倍的大小,这段程序的运行速度确实慢了些。(用一台服务器,跑一整天时间都执行不完。)

[ Last edited by cny798 on 2010-7-24 at 16:06 ]
作者: defanive     时间: 2010-7-24 17:16
这样你还是用第三方吧。。。
我只想看还有没别的方法。。。
作者: netbenton     时间: 2010-7-24 19:40
setlocal disabledelayedexpansion
哦~~这个是从你那里粘贴来的,为什么要disabel呢,难道文本里面不有!号吗?
改成
setlocal enabledelayedexpansion
应该可以了的,可是在速度上来说,你还是用三方吧
作者: cny798     时间: 2010-7-24 23:07
用VBS就能又快又好,只是不知道批处理可否。

附上VBS源码:


content1="~|"
content2="☆"
name1="1.txt"
name2="2.txt"

set fso=createobject("scripting.filesystemobject")

set file=fso.opentextfile(name1,1)
if file.AtEndOfStream<>true then
content=file.readall
file.close
content=replace(content,content1,content2)
set file=fso.opentextfile(name2,2)
file.write content
file.close
else
file.close
end if