Board logo

标题: [文本处理] [已解决]批处理如何导出TXT文件中包含某字词的行? [打印本页]

作者: yaua    时间: 2011-8-6 14:55     标题: [已解决]批处理如何导出TXT文件中包含某字词的行?

本帖最后由 yaua 于 2011-8-7 14:29 编辑

http://www.bathome.net/viewthread.php?from=notice&tid=12715
源代码在这个帖子中,但试验一个txt,发现导出的行不完整
比如原始txt为
  1. (005321) 8/6/2011 14:07:48 PM - (not logged in) (125.43.38.191)> 331 Password required for anonymous
  2. (005321) 8/6/2011 14:07:51 PM - (not logged in) (125.43.38.191)> PASS *******
  3. (005321) 8/6/2011 14:07:51 PM - (not logged in) (125.43.38.191)> 530 Login or password incorrect!
  4. (005321) 8/6/2011 14:07:51 PM - (not logged in) (125.43.38.191)> QUIT
  5. (005321) 8/6/2011 14:07:51 PM - (not logged in) (125.43.38.191)> 221 Goodbye
  6. (005321) 8/6/2011 14:07:51 PM - (not logged in) (125.43.38.191)> disconnected.
  7. (005322) 8/6/2011 14:08:08 PM - (not logged in) (125.43.38.191)> Connected, sending welcome message...
  8. (005322) 8/6/2011 14:08:08 PM - (not logged in) (125.43.38.191)> 220-FileZilla Server version 0.9.39 beta
  9. (005322) 8/6/2011 14:08:08 PM - (not logged in) (125.43.38.191)> 220-written by Tim Kosse (Tim.Kosse@gmx.de)
  10. (005322) 8/6/2011 14:08:08 PM - (not logged in) (125.43.38.191)> 220 Please visit http://sourceforge.net/projects/filezilla/
  11. (005322) 8/6/2011 14:08:09 PM - (not logged in) (125.43.38.191)> USER anonymous
  12. (005322) 8/6/2011 14:08:09 PM - (not logged in) (125.43.38.191)> 331 Password required for anonymous
  13. (005322) 8/6/2011 14:08:09 PM - (not logged in) (125.43.38.191)> PASS *******
  14. (005322) 8/6/2011 14:08:09 PM - (not logged in) (125.43.38.191)> 421 Temporarily banned for too many failed login attempts
  15. (005322) 8/6/2011 14:08:09 PM - (not logged in) (125.43.38.191)> disconnected.
复制代码
使用代码为
  1. @echo off
  2. set "str=220"
  3. (for /f "tokens=1* delims=:" %%a in ('findstr "%str%" "FileZilla Server.log"') do (
  4.     echo,%%b
  5. ))>111.log
复制代码
导出的内容为
  1. 08:08 PM - (not logged in) (125.43.38.191)> 220-FileZilla Server version 0.9.39 beta
  2. 08:08 PM - (not logged in) (125.43.38.191)> 220-written by Tim Kosse (Tim.Kosse@gmx.de)
  3. 08:08 PM - (not logged in) (125.43.38.191)> 220 Please visit http://sourceforge.net/projects/filezilla/
复制代码
似乎前面的日期并没有完整导出,是何原因
还有个问题,要是查找的字符不知一个,比如 220 或 not 两个关键词,又该如何修改代码
作者: 601997526    时间: 2011-8-6 15:15

本帖最后由 601997526 于 2011-8-6 15:25 编辑

"tokens=1* delims=:"这一句改下就行了
  1. @echo off
  2. set "str=220 not"
  3. (for /f "delims=" %%a in ('findstr "%str%" "FileZilla Server.log"') do (
  4.     echo,%%a
  5. ))>111.log
复制代码
也可以改为"tokens=* delims=:"
要不就在findstr后面加上 /n
要查找多个关键字就直接在set str=这里加上关键字和一个空格就行了
作者: yaua    时间: 2011-8-6 16:25

如果查找的关键字中含有空格呢
作者: CUer    时间: 2011-8-6 19:41

回复 4# yaua
  1. findstr /c:"abc def"
复制代码

作者: yaua    时间: 2011-8-6 20:57

关键字不止一个,且其中一个含有空格。
比如关键字 "a b" 和 "c"
作者: hanyeguxing    时间: 2011-8-7 09:07

  1. findstr /c:"a b" /c:"c"
复制代码





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