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

[文本处理] 请大神指教:BAT批量提取N个文本的关键词下一行

[复制链接]
发表于 2022-10-10 23:21:34 | 显示全部楼层 |阅读模式
有一堆文档,里面有: Did You Know? 字样,想提取这一行的下一行,搜索了一下论坛中的代码,没有成功。
请大神帮忙!

            
            
            <!--2-nd part of word-->
              <div class="did-you-know-wrapper">
                  <h2>Did You Know?</h2>
                  <p>If someone's duplicity has left you feeling like you're seeing double, take heart in the word's etymology. "Duplicity" comes from a long line of "double" talk, starting with its Latin grandparent "duplex," which means "double" or "twofold." As you might expect, "duplex" is also the parent of another term for doubling it up, "duplicate." And of course, the English "duplex" (which can be a noun meaning "a two-family house" or an adjective meaning "double") comes from the Latin word of the same spelling.</p>
                  <span class="scrollDepth" data-eventName="wotd-did-you-know"></span>                  
              </div>
              <hr class="blue-divide thin-divide no-float">
              <div class="game-recirc-widget">
发表于 2022-10-11 00:49:53 | 显示全部楼层
有一堆文档,里面有: Did You Know? 字样,想提取这一行的下一行,搜索了一下论坛中的代码,没有成功。
请 ...
ronger28 发表于 2022-10-10 23:21



照着其他大佬的代码写的,文本存在test.txt里面
  1. <# :
  2. @echo off&powershell -NoProfile -ExecutionPolicy bypass "Get-Content -literal '%~f0'|Out-String|Invoke-Expression"&pause
  3. #>
  4. $n=0;gc .\test.txt |%{if($_.contains("Did You Know?")){(gc .\test.txt)[$n+1];break}else{$n+=1}}
复制代码
 楼主| 发表于 2022-10-11 06:54:28 | 显示全部楼层
您好,感谢您给我回帖 http://www.bathome.net/viewthrea ... =%CF%C2%D2%BB%D0%D0 ,我刚才激动地试了一下这个代码,但输出的是空白。我录了一段视频,能否让您费心帮我看一下哪个环节出现错误了。非常感谢!

链接:https://pan.baidu.com/s/11c0N0nR0wkQwBfo7QkeVkg?pwd=gehq
提取码:gehq
发表于 2022-10-11 07:47:31 | 显示全部楼层
如果文本都是这样的,按楼主的方法,对我来说需要更多时间来调整代码。但是依据现有样例,可以很简单
1利用网页关键词<p>
2利用词频,英文的the频度很高
  1. for /f "delims=" %%i in ('dir /b/on *.txt') do findstr /i "^<p^> the" %%i>>结果%%i
复制代码
发表于 2022-10-11 08:28:46 | 显示全部楼层
回复 3# ronger28

要处理的文件改成test.txt,不是结果在test.txt
发表于 2022-10-11 08:40:35 | 显示全部楼层
如不介意用第3方工具,可用gawk(http://bcn.bathome.net/tool/4.1.0/gawk.exe),指令如下:

  1. gawk "A;A=0;/Did You Know\?/{A=1}" test.txt
复制代码
发表于 2022-10-11 08:52:58 | 显示全部楼层
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. for /f "delims=" %%a in ('type *.txt 2^>nul') do (
  4.     if not "!str1!"=="!str2!" echo,%%a
  5.     set "str1=%%a"
  6.     set "str2=!str1:Did You Know?=!"
  7. )
  8. pause
复制代码
发表于 2022-10-11 09:02:56 | 显示全部楼层
回复 1# ronger28


    建议把原始文件上传到网盘,方便大家测试代码。
发表于 2022-10-11 09:05:28 | 显示全部楼层
3楼有链接。
发表于 2022-10-11 09:13:40 | 显示全部楼层
本帖最后由 xp3000 于 2022-10-11 09:19 编辑
  1. @if(0)==(0) echo off&setlocal EnableDelayedExpansion&cd %~dp0
  2. echo 保存ANSI格式BAT文件,处理ANSI文件

  3. @for /f "delims=" %%a in ('dir /b/a-d/oN "*.txt"')do (

  4.     for /f "delims=" %%b in ('type "%%a"^|cscript -nologo -e:jscript "%~0"')do (
  5.     echo %%b
  6.     echo.
  7.     )
  8. )
  9. pause&exit /b
  10. @end

  11. var text = WScript.StdIn.ReadAll().replace(/[\s\S]*Did You Know.+[\r\n]+(.+)[\r\n]+[\s\S]*/g, '$1')
  12. WScript.Echo(text.match(/.+/g).join('\r\n'))
复制代码
可以修改*.txt为其他,输出的话 echo.和echo %%b后面加>>.xx.xxx文件或>>"%%a.xxx"
 楼主| 发表于 2022-10-11 21:55:39 | 显示全部楼层
回复 10# xp3000

完美解决问题,看着5000多条数据像跑火车一样,非常开心!非常感谢!向您学习!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-3-19 14:43 , Processed in 0.021206 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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