[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[文本处理] [已解决]批处理如何导出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 两个关键词,又该如何修改代码
1

评分人数

    • CrLf: 感谢给帖子标题标注[已解决]字样PB + 2

本帖最后由 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=这里加上关键字和一个空格就行了
1

评分人数

    • CrLf: 乐于助人技术 + 1

TOP

如果查找的关键字中含有空格呢

TOP

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

评分人数

    • CrLf: 乐于助人技术 + 1

TOP

关键字不止一个,且其中一个含有空格。
比如关键字 "a b" 和 "c"

TOP

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

评分人数

    • CrLf: 乐于助人技术 + 1
寒夜孤星:在没有说明的情况下,本人所有代码均运行在 XP SP3 下 (有问题请发贴,QQ临时会话已关闭)

TOP

返回列表