标题: [文本处理] [已解决]批处理如何导出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为- (005321) 8/6/2011 14:07:48 PM - (not logged in) (125.43.38.191)> 331 Password required for anonymous
- (005321) 8/6/2011 14:07:51 PM - (not logged in) (125.43.38.191)> PASS *******
- (005321) 8/6/2011 14:07:51 PM - (not logged in) (125.43.38.191)> 530 Login or password incorrect!
- (005321) 8/6/2011 14:07:51 PM - (not logged in) (125.43.38.191)> QUIT
- (005321) 8/6/2011 14:07:51 PM - (not logged in) (125.43.38.191)> 221 Goodbye
- (005321) 8/6/2011 14:07:51 PM - (not logged in) (125.43.38.191)> disconnected.
- (005322) 8/6/2011 14:08:08 PM - (not logged in) (125.43.38.191)> Connected, sending welcome message...
- (005322) 8/6/2011 14:08:08 PM - (not logged in) (125.43.38.191)> 220-FileZilla Server version 0.9.39 beta
- (005322) 8/6/2011 14:08:08 PM - (not logged in) (125.43.38.191)> 220-written by Tim Kosse (Tim.Kosse@gmx.de)
- (005322) 8/6/2011 14:08:08 PM - (not logged in) (125.43.38.191)> 220 Please visit http://sourceforge.net/projects/filezilla/
- (005322) 8/6/2011 14:08:09 PM - (not logged in) (125.43.38.191)> USER anonymous
- (005322) 8/6/2011 14:08:09 PM - (not logged in) (125.43.38.191)> 331 Password required for anonymous
- (005322) 8/6/2011 14:08:09 PM - (not logged in) (125.43.38.191)> PASS *******
- (005322) 8/6/2011 14:08:09 PM - (not logged in) (125.43.38.191)> 421 Temporarily banned for too many failed login attempts
- (005322) 8/6/2011 14:08:09 PM - (not logged in) (125.43.38.191)> disconnected.
复制代码
使用代码为-
- @echo off
- set "str=220"
- (for /f "tokens=1* delims=:" %%a in ('findstr "%str%" "FileZilla Server.log"') do (
- echo,%%b
- ))>111.log
复制代码
导出的内容为-
- 08:08 PM - (not logged in) (125.43.38.191)> 220-FileZilla Server version 0.9.39 beta
- 08:08 PM - (not logged in) (125.43.38.191)> 220-written by Tim Kosse (Tim.Kosse@gmx.de)
- 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=:"这一句改下就行了- @echo off
- set "str=220 not"
- (for /f "delims=" %%a in ('findstr "%str%" "FileZilla Server.log"') do (
- echo,%%a
- ))>111.log
复制代码
也可以改为"tokens=* delims=:"
要不就在findstr后面加上 /n
要查找多个关键字就直接在set str=这里加上关键字和一个空格就行了
作者: yaua 时间: 2011-8-6 16:25
如果查找的关键字中含有空格呢
作者: CUer 时间: 2011-8-6 19:41
回复 4# yaua 复制代码
作者: yaua 时间: 2011-8-6 20:57
关键字不止一个,且其中一个含有空格。
比如关键字 "a b" 和 "c"
作者: hanyeguxing 时间: 2011-8-7 09:07
复制代码
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |