Board logo

标题: [出题]字符替换 [打印本页]

作者: moniuming     时间: 2008-11-21 22:10    标题: [出题]字符替换

只限新手
现有两个文本,分别为test1.txt和test2.txt,内容如下:
test1.txt(有特殊字符,有空行)
CODE:  [Copy to clipboard]
Tell Me Why
演唱: Declan Galbraith
制作: Eric ,for Crystal

In my dream,children sing
A song of love for every boy and girl
The sky is blue and fields are green
And laughter is the language of the world
Then i wake and all i see
Is a world full of people in need

Tell me why (why) does it have to be like this?
Tell me why (why) is there something i have missed?
Tell me why (why) cos i don’t understand
When so many need somebody
We don’t give a helping hand Tell me why?

Everyday i ask myself
What will i have to do to be a man?
Do i have to stand and fight
To prove to everybody who i am?
Is that what my life is for
To waste in a world full of war?

Tell me why (why) does it have to be like this?
Tell me why (why) is there something i have missed?
Tell me why (why) cos i don’t understand
When so many need somebody
We don’t give a helping hand Tell me why?
(children)tell me why?(declan)tell me why?
(children)tell me why?(declan)tell me why?
(together) just tell me why, why, why?

Tell me why (why) does it have to be like this?
Tell me why (why) is there something i have missed?
Tell me why (why) cos i don’t understand
When so many need somebody
We don’t give a helping hand

Tell me why (why,why,does the tiger run)
Tell me why(why why do we shoot the gun)
Tell me why (why,why do we never learn)
Can someone tell us why we let the forest burn?
(why,why do we say we care)
Tell me why(why,why do we stand and stare)
Tell me why(why,why do the dolphins cry)
Can some one tell us why we let the ocean die?
(why,why if we’re all the same)
Tell me why(why,why do we pass the blame)
Tell me why (why,why does it never end)
Can some one tell us why we cannot just be friends?
Why,why,(do we close our eyes)
Why,why,(do the greedy life)
Why,why,(do we fight for land)
Can someone tell us why 'cos we don't understand?
Why,why?!
这里有特殊字符:~!@#$%^&*()_+|\=-`}{[]';:"/.,<>?
-END-
test2.txt(每个字符之间用半角空格隔开,有多行,没有特殊字符)
CODE:  [Copy to clipboard]
c n
d o s
b a t
特 殊
现要求如下:
    在test1.txt中,只要存在test2.txt中的字符,则把他们全都用"★"号代替,效果如下:

  Quote:
★ell Me Why
演唱: ★e★l★★ G★l★r★i★h
制作: Eri★ ,f★r ★ry★★★l

I★ my ★re★m,★hil★re★ ★i★g
★ ★★★g ★f l★ve f★r every ★★y ★★★ girl
★he ★ky i★ ★lue ★★★ fiel★★ ★re gree★
★★★ l★ugh★er i★ ★he l★★gu★ge ★f ★he w★rl★
★he★ i w★ke ★★★ ★ll i ★ee
I★ ★ w★rl★ full ★f pe★ple i★ ★ee★

★ell me why (why) ★★e★ i★ h★ve ★★ ★e like ★hi★?
★ell me why (why) i★ ★here ★★me★hi★g i h★ve mi★★e★?
★ell me why (why) ★★★ i ★★★’★ u★★er★★★★★
Whe★ ★★ m★★y ★ee★ ★★me★★★y
We ★★★’★ give ★ helpi★g h★★★ ★ell me why?

Every★★y i ★★k my★elf
Wh★★ will i h★ve ★★ ★★ ★★ ★e ★ m★★?
★★ i h★ve ★★ ★★★★★ ★★★ figh★
★★ pr★ve ★★ every★★★y wh★ i ★m?
I★ ★h★★ wh★★ my life i★ f★r
★★ w★★★e i★ ★ w★rl★ full ★f w★r?

★ell me why (why) ★★e★ i★ h★ve ★★ ★e like ★hi★?
★ell me why (why) i★ ★here ★★me★hi★g i h★ve mi★★e★?
★ell me why (why) ★★★ i ★★★’★ u★★er★★★★★
Whe★ ★★ m★★y ★ee★ ★★me★★★y
We ★★★’★ give ★ helpi★g h★★★ ★ell me why?
(★hil★re★)★ell me why?(★e★l★★)★ell me why?
(★hil★re★)★ell me why?(★e★l★★)★ell me why?
(★★ge★her) ju★★ ★ell me why, why, why?

★ell me why (why) ★★e★ i★ h★ve ★★ ★e like ★hi★?
★ell me why (why) i★ ★here ★★me★hi★g i h★ve mi★★e★?
★ell me why (why) ★★★ i ★★★’★ u★★er★★★★★
Whe★ ★★ m★★y ★ee★ ★★me★★★y
We ★★★’★ give ★ helpi★g h★★★

★ell me why (why,why,★★e★ ★he ★iger ru★)
★ell me why(why why ★★ we ★h★★★ ★he gu★)
★ell me why (why,why ★★ we ★ever le★r★)
★★★ ★★me★★e ★ell u★ why we le★ ★he f★re★★ ★ur★?
(why,why ★★ we ★★y we ★★re)
★ell me why(why,why ★★ we ★★★★★ ★★★ ★★★re)
★ell me why(why,why ★★ ★he ★★lphi★★ ★ry)
★★★ ★★me ★★e ★ell u★ why we le★ ★he ★★e★★ ★ie?
(why,why if we’re ★ll ★he ★★me)
★ell me why(why,why ★★ we p★★★ ★he ★l★me)
★ell me why (why,why ★★e★ i★ ★ever e★★)
★★★ ★★me ★★e ★ell u★ why we ★★★★★★ ju★★ ★e frie★★★?
Why,why,(★★ we ★l★★e ★ur eye★)
Why,why,(★★ ★he gree★y life)
Why,why,(★★ we figh★ f★r l★★★)
★★★ ★★me★★e ★ell u★ why '★★★ we ★★★'★ u★★er★★★★★?
Why,why?!
这里有★★字符:~!@#$%^&*()_+|\=-`}{[]';:"/.,<>?
-E★★-


作者: Saber     时间: 2008-11-22 00:18
CODE:  [Copy to clipboard]
@ECHO OFF
FOR /F "delims=" %%a IN (Text1.txt) DO (
        Set "Text=%%a"
        SETLOCAL ENABLEDELAYEDEXPANSION
        FOR /F "tokens=*" %%i IN (Text2.txt) DO (
                FOR %%a IN (%%i) DO Set Text=!Text:%%a=★!
        )
        ECHO !Text!
        ENDLOCAL
)
Pause

==========EDIT==========
@ECHO OFF
FOR /F "delims=" %%a IN ('FindStr /N .* Text1.txt') DO (
        Set "Text=%%a"
        SETLOCAL ENABLEDELAYEDEXPANSION
        Set Text=!Text:*:=!
        IF NOT "!Text!"=="" FOR /F "tokens=*" %%i IN (Text2.txt) DO (
                FOR %%a IN (%%i) DO Set Text=!Text:%%a=★!
        )
        ECHO\!Text!
        ENDLOCAL
)
Pause
==========EDIT==========


[ Last edited by Saber on 2008-11-22 at 01:48 ]
作者: moniuming     时间: 2008-11-22 00:30
2楼完成得太好了,可惜没输出空行,要不可以称得上完美了,今天的分已经加完,过后补上...

[ Last edited by moniuming on 2008-11-22 at 00:38 ]
作者: Saber     时间: 2008-11-22 00:40
没注意……居然还有空行。
刚刚又去看了下前辈写的,果然是一个命令都不能少啊……

[ Last edited by Saber on 2008-11-22 at 00:48 ]
作者: cailinfeng     时间: 2008-11-22 00:53
好笨啊我!想了好久只想到这种办法了!
@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%i in (1.txt) do (set a=%%i
  set b=!a:c=★!
  set b=!b:n=★!
  set b=!b:d=★!
  set b=!b:o=★!
  set b=!b:s=★!
  set b=!b:b=★!
  set b=!b:a=★!
  set b=!b:t=★!
  if /i "!b!"=="why,why?" endlocal&goto :1
  echo !b!)
:1
for /f "skip=55 delims=" %%j in (1.txt) do echo %%j
pause
作者: pigpigs     时间: 2008-11-22 10:47
Set Text=!Text:*:=!
这句在这里起的什么作用呀?
作者: tireless     时间: 2008-11-22 11:13


  Quote:
Originally posted by pigpigs at 2008-11-22 10:47 AM:
Set Text=!Text:*:=!
这句在这里起的什么作用呀?

例如变量 Text 的值为 123:abc,执行这句后变量 Text 的值就变为了 abc
作者: HAT     时间: 2008-11-22 12:54    标题: Re 6楼

多看帮助:
set /?
环境变量替换已如下增强:

    %PATH:str1=str2%

会扩展 PATH 环境变量,用 "str2" 代替扩展结果中的每个 "str1"。
要有效地从扩展结果中删除所有的 "str1","str2" 可以是空的。
"str1" 可以以星号打头;在这种情况下,"str1" 会从扩展结果的
开始到 str1 剩余部分第一次出现的地方,都一直保持相配。

作者: hackate     时间: 2008-11-22 13:23
@ECHO OFF
FOR /F "delims=" %%a IN ('findstr /n .* chuli.txt') DO (
        Set "Text=%%a"
        SETLOCAL ENABLEDELAYEDEXPANSION
        set "Text=!Text:*:=!
        if not "!Text!"=="" (
        FOR /F "tokens=*" %%i IN (b.txt) DO (
                FOR %%a IN (%%i) DO Set Text=!Text:%%a=★!
        )
        )
        ECHO.!Text!
        ENDLOCAL
)
Pause


我也来答个.拿楼上朋友的代码稍微改了下,现在可以把空行也输出了
作者: netbenton     时间: 2009-2-25 23:55    标题: 这样可能快一点

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
set b=
FOR /F "tokens=*" %%a IN (b.txt) DO set "b=!b! %%a"
ENDLOCAL&set b=%b%

FOR /F "delims=" %%a IN ('findstr /n .* chuli.txt') DO (
        Set "str=%%a"
        SETLOCAL ENABLEDELAYEDEXPANSION
        FOR %%i IN (%b%) DO Set "str=!str:%%i=★!"
        echo.!str!
        ENDLOCAL
)
Pause




欢迎光临 中国DOS联盟论坛 (http://cndos.fam.cx/forum/) Powered by Discuz! 2.5