Board logo

标题: 【练习-018】按要求整理并改写文本 [打印本页]

作者: batman    时间: 2008-8-30 10:08     标题: 【练习-018】按要求整理并改写文本

出题目的:
        掌握字符截取技巧
        掌握学符替换技巧
        掌握改写文本技巧        
解题要求:
        代码通用、高效
        尽量简洁
        不生成临时文件
加分规则:
         思路独特基准分5分
        代码高效、通用基准分4分
        技巧高超基准分3分
        代码简洁基准分2分
        完美代码加分15分           
题目如下:
  有文本a.txt
  1. Network vertically: In the research phase of your job hunt,talk to
  2. people who are on a level above you in your desired industry. They'll
  3. have some insights that people at your own level won't have,and will be
  4. in a good position to hire you or recommend you to be hired. Keep your
  5. spirits up. Looking for a job is one of the toughest things you will
  6. ever have to do. Maintain your confidence,stay persistent,and think
  7. positively,and eventually you will get a job that suits you.
复制代码
1 要求通过批处理将文本中的you全替换成me并输出,但your不能变
2 要求通过批处理将文本整理成如下格式(遇:号和.号换行并在行首空两格),并生成文本b.txt:
  1. Network vertically:
  2.   In the research phase of your job hunt,talk to people who are on a level abov
  3. e me in your desired industry.
  4.   They'll have some insights that people at your own level won't have,and will
  5. be in a good position to hire me or recommend me to be hired.
  6.   Keep your spirits up.
  7.   Looking for a job is one of the toughest things me will ever have to do.
  8.   Maintain your confidence,stay persistent,and think positively,and eventuall
  9. y me will get a job that suits me.
复制代码
3 要求将文本b.txt中的Looking for a job is one of the toughest things me will ever have to do.
这句改写成bathomework is a nice job,其它的不变。
解题限制:
  高手请暂做旁观,请新手老手解答。
补充两点:
       1 a.txt中只有一行
       2 解题中不使用第三方工具(并不是提倡不使用第三方)

[ 本帖最后由 batman 于 2008-8-30 13:00 编辑 ]
作者: yslyxqysl    时间: 2008-8-30 10:42

  1. @echo off
  2. setlocal ENABLEDELAYEDEXPANSION
  3. for /f "tokens=*" %%i in (a.txt) do (
  4. set i=%%i
  5. set i=!i: you = me !
  6. set i=!i: you.= me.!
  7. set i=!i: you,= me,!
  8. set i=!i: you?= me?!
  9. set i=!i: you^^^!= me^^^!!
  10. echo !i!>>a.txt
  11. )
复制代码

[ 本帖最后由 yslyxqysl 于 2008-9-19 20:33 编辑 ]
作者: batman    时间: 2008-8-30 10:51

楼上的出现了书写错误吧:@echo,off
作者: wangwei4106    时间: 2008-8-30 11:04

2楼代码有误,至少最后一个you.就替换不了!
这样应该就可以了!
  1. @echo off&setlocal enabledelayedexpansion
  2. for /f "tokens=*" %%i in (a.txt) do (
  3. set aa=%%i
  4. set aa=!aa:you=me!
  5. set aa=!aa:mer=your!
  6. echo !aa!
  7. )
  8. pause>nul
复制代码


呵呵,谢谢batman
为什么我老是出错呢!

[ 本帖最后由 wangwei4106 于 2008-8-30 15:15 编辑 ]
作者: yslyxqysl    时间: 2008-8-30 11:04

加个逗号是为了测试,嘻嘻。
作者: yslyxqysl    时间: 2008-8-30 11:08

当文本出现young的时候,4楼的代码有些麻烦
作者: wangwei4106    时间: 2008-8-30 11:09

郁闷!
呵呵
我也没有想更多了!
谢谢提醒啊,我再去看看!
作者: Batcher    时间: 2008-8-30 11:30

第一题:
  1. sed "s/you /me /g" a.txt
复制代码
Unix for Windows 工具集
http://bbs.bathome.net/thread-1114-1-1.html
作者: Batcher    时间: 2008-8-30 11:42

第二题:
  1. @echo off
  2. copy a.txt b.txt
  3. change b.txt /from ":" /to ":&H0D0A  "
  4. change b.txt /from "." /to ".&H0D0A  "
复制代码
文本修改工具change.exe
http://bbs.bathome.net/viewthread.php?tid=1685
作者: wangwei4106    时间: 2008-8-30 11:53

不知道你们搞这个工具出来,是不是有点离题了……
其实我还不会用这些个工具!^_^
作者: batman    时间: 2008-8-30 12:57

原帖由 wangwei4106 于 2008-8-30 11:04 发表
2楼代码有误,至少最后一个you.就替换不了!
这样应该就可以了!
@echo off&setlocal enabledelayedexpansion
for /f "tokens=* delims= " %%i in (a.txt) do (
set aa=%%i
set aa=!aa:you=me!
set aa=!aa:mer ...

为什么要tokens=* delims= 呢?
作者: terse    时间: 2008-8-30 13:06

写出了  晚上来贴
现在就Looking for a job is one of the toughest things me will ever have to do.
这句里will ever的空格有不?因为我这里替换没成功 去了空格就可以

[ 本帖最后由 terse 于 2008-8-30 13:08 编辑 ]
作者: more    时间: 2008-8-30 14:44

帖上两个(都不怎么通用),不知哪个会得到版主的认可,或都不合???
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. for /f "delims=" %%a in (test.txt) do (
  4.    call set "str=%%str%%%%a"
  5. )
  6. :lp
  7. for /f "tokens=1* delims=:." %%a in ("%str%") do (
  8.    if not defined home (echo %%a:&set home=bat) else (
  9.       set "var= %%a."
  10.       if /i "!var:~2,7!"=="looking" (echo   Bathomework is a nice job.) else (
  11.          set "var=!var:you =me !"&set "var=!var:you.=me.!"&echo !var!
  12.       )
  13.    )
  14.    if not "%%b"=="" set "str=%%b"&goto :lp
  15. )
  16. pause>nul
复制代码
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. for /f "delims=" %%a in (test.txt) do (
  4.    set "str=%%a"
  5.    call :lp
  6. )
  7. pause>nul
  8. goto :eof
  9. :lp
  10. for /f "tokens=1*" %%i in ("%str%") do (
  11.    set "v=%%i"
  12.    if "!v!"=="you" set "v=me"
  13.    if "!v!"=="you." set "v=me."
  14.    call set "var=%%var%%%%v%% "
  15.    if "!var:~-2!"==": " (echo !var!&set "var=")
  16.    if "!var:~-2!"==". " (
  17.       if /i "!var:~,7!"=="looking" (
  18.          echo   Bathomework is a nice job.&set "var="
  19.       ) else (echo   !var!&set "var=")
  20.    )
  21.    if not "%%j"=="" set "str=%%j"&goto :lp
  22. )
复制代码

作者: pusofalse    时间: 2008-8-30 15:07     标题: 回复 13楼 的帖子

兄的代码是否考虑过于复杂了。
------------------------------------
没想到改题,屏蔽原有内容。

[ 本帖最后由 pusofalse 于 2008-8-30 15:21 编辑 ]
作者: batman    时间: 2008-8-30 15:24

提示:考虑问题复杂化,解决问题简洁化
作者: terse    时间: 2008-8-30 17:16

延迟出错 更改
  1. @echo off
  2. for /f "tokens=*" %%i in (a.txt) do if not defined str (set str=%%i)else call set str=%%str%% %%i
  3.     set str=%str:your=_%
  4.     set str=%str:you=me%
  5.     set str=%str:_=your%
  6.     set str=%str::=:#%
  7.     set str=%str:.=.#%
  8.     set str=%str:Looking for a job is one of the toughest things me will ever have to do.=bathomework is a nice job,%
  9.     set str=%str:#=^&^>^>b.txt echo. %
  10.     >>b.txt echo.%str%
  11.     pause
复制代码


[ 本帖最后由 terse 于 2008-8-30 20:45 编辑 ]
作者: 随风    时间: 2008-8-30 17:56

1
  1. @echo off
  2. for /f "delims=" %%a in (a.txt) do call set "str=%%str%%%%a"
  3. set "str= %str% "
  4. set "str=%str: you = me %"
  5. set "str=%str: you. = me. %"
  6. echo %str:~1,-1%
  7. pause
复制代码
2
  1. @echo off
  2. for /f "delims=" %%a in (a.txt) do call set "str=%%str%%%%a"
  3. set "str=%str::=:&echo\ %"
  4. set "str=%str:.=.&echo\ %"
  5. (echo %str%)>>b.txt
  6. start "" "b.txt"
复制代码


[ 本帖最后由 随风 于 2008-8-30 17:57 编辑 ]
作者: wangwei4106    时间: 2008-8-31 22:06

才发现set功能如此强大,看了这贴子并参与进去,学到了很多!
要是有多些这样的训练就好了,自己想训练自己可是没有一点目标!
更加也发现不了自己的缺项,希望多点讲解……
貌似你们就发了贴,还不明白为什么就要这样用……
作者: keen    时间: 2009-5-14 00:20

  1. @echo off&setlocal enabledelayedexpansion
  2. set /p str=<1.txt
  3. for %%a in (%str%) do (
  4.         set var=%%a
  5.         set a=!var:~0,4!
  6.         if "!a!"=="you" set var=!var:you=me!
  7.         set "str1=!str1!!var! "
  8. )
  9. set "str1=%str1::=:&echo\ %"
  10. set "str1=%str1:.=.&echo\ %"
  11. echo %str1:you.=me.%&pause
复制代码

[ 本帖最后由 keen 于 2009-5-14 14:04 编辑 ]
作者: lengmoke    时间: 2021-10-14 11:44

  1. @echo off
  2. for /f "delims=" %%i in (a.txt) do ( set "str=%%i" )
  3. set str=%str:your=####%
  4. set str=%str:you=me%
  5. set str=%str:####=your%
  6. set str=%str::=:^^^&echo, %
  7. set str=%str:.=.^&echo,  %
  8. (echo %str%)>>b.txt
  9. for /f "delims=" %%i in (b.txt) do (
  10.     if "%%i"=="  Looking for a job is one of the toughest things me will ever have to do." (
  11.         echo,  bathomework is a nice job.
  12.     ) else (
  13.         echo,%%i
  14.     )
  15. )
复制代码





欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2