找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 28045|回复: 4

[文本处理] [已解决]批处理如何判断txt最后10行是否包含某些字符串并输出这些行?

[复制链接]
发表于 2018-6-30 23:06:54 | 显示全部楼层 |阅读模式
判断一较大文件 123.txt的最后10行:如果含有字符串“错误” 和“AutoConfig”,则把该文件复制到指定位置d:\456.txt;
提取456.txt的倒数第4行的前6个字符,并输出最后20行中包含该6个字符串的所有行到一个文件d:\789.txt
批处理能快速做到这些吗?请大家帮忙,谢谢!

123.txt的内容
信息 2018/6/30 22:13:09 Microsoft-Windows-Kernel-General 1 无
信息 2018/6/30 22:11:10 Microsoft-Windows-Kernel-General 1 无
信息 2018/6/30 22:07:57 Service Control Manager
信息 2018/6/30 22:03:10 Service Control Manager
信息 2018/6/30 22:03:10 Service Control Manager
错误 2018/6/30 22:03:10 Service Control Manager
错误 2018/6/30 22:03:10 Service Control Manager
信息 2018/6/30 22:03:09 Microsoft-Windows-Kernel-General 1 无
信息 2018/6/30 22:03:07 Service Control Manager
信息 2018/6/30 22:03:05 Service Control Manager
信息 2018/6/30 22:03:05 Microsoft-Windows-WLAN-AutoConfig 4000 无
信息 2018/6/30 22:03:05 Service Control Manager
错误 2018/6/30 22:03:05 Service Control Manager
信息 2018/6/30 7:08:02 Service Control Manager
信息 2018/6/30 7:08:02 Service Control Manager
信息 2018/6/30 7:08:00 Microsoft-Windows-Winlogon 7001 -1101
信息 2018/6/30 7:08:00 Service Control Manager
信息 2018/6/30 7:08:00 Microsoft-Windows-WLAN-AutoConfig 4000 无
信息 2018/6/30 7:08:00 Service Control Manager
信息 2018/6/30 7:08:00 Microsoft-Windows-DHCPv6-Client 51046 服务状态事件
错误 2018/6/30 7:08:00 Service Control Manager
信息 2018/6/30 7:08:00 Microsoft-Windows-Dhcp-Client 50036 服务状态事件
信息 2018/6/30 7:08:00 Service Control Manager
信息 2018/6/30 7:08:00 Service Control Manager

评分

参与人数 1PB +2 收起 理由
Batcher + 2 感谢给帖子标题标注[已解决]字样

查看全部评分

发表于 2018-7-2 11:51:14 | 显示全部楼层
论坛内已经有好多类似的帖子,楼主自己先搜下,尝试自己解决吧
发表于 2018-7-2 12:21:11 | 显示全部楼层
个人感觉这种纯P困难,就用python写了个 用不了几行可以搞定。不过大文件没测试 没怎么考虑效率。
  1. import shutil

  2. str1 = "错误"
  3. str2 = "AutoConfig"
  4. file = "123.txt"
  5. file2 = "d:\\456.txt"
  6. file3 = "d:\\789.txt"

  7. with open(file,encoding="utf-8") as f:
  8.     last10 = "".join(list(f)[-10:])

  9. if str1 in last10 and str2 in last10:
  10.     shutil.copy(file, file2)
  11.     with open(file2,encoding="utf-8") as f:
  12.         flst = list(f)
  13.         last4th6chars = flst[-4][:6]    #前6个是  "错误 201"
  14.         with open(file3,"w",encoding="utf-8") as fw:
  15.             for line in flst[-20:]:
  16.                 if last4th6chars in line: fw.write(line)
复制代码
发表于 2018-7-2 12:38:44 | 显示全部楼层
以顶楼数据为例,提取456.txt的倒数第4行的前6个字符,希望得到的结果是什么?
 楼主| 发表于 2018-7-21 11:29:44 | 显示全部楼层
回复 3# cfwyy77_bat


    谢谢回复,已经解决!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-18 12:11 , Processed in 0.019874 second(s), 9 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表