Board logo

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

作者: ronger28    时间: 2022-10-10 23:21     标题: 请大神指教:BAT批量提取N个文本的关键词下一行

有一堆文档,里面有: 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">
作者: pd1    时间: 2022-10-11 00:49

有一堆文档,里面有: 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}}
复制代码

作者: ronger28    时间: 2022-10-11 06:54

您好,感谢您给我回帖 http://www.bathome.net/viewthrea ... =%CF%C2%D2%BB%D0%D0 ,我刚才激动地试了一下这个代码,但输出的是空白。我录了一段视频,能否让您费心帮我看一下哪个环节出现错误了。非常感谢!

链接:https://pan.baidu.com/s/11c0N0nR0wkQwBfo7QkeVkg?pwd=gehq
提取码:gehq
作者: hlzj88    时间: 2022-10-11 07:47

如果文本都是这样的,按楼主的方法,对我来说需要更多时间来调整代码。但是依据现有样例,可以很简单
1利用网页关键词<p>
2利用词频,英文的the频度很高
  1. for /f "delims=" %%i in ('dir /b/on *.txt') do findstr /i "^<p^> the" %%i>>结果%%i
复制代码

作者: pd1    时间: 2022-10-11 08:28

回复 3# ronger28

要处理的文件改成test.txt,不是结果在test.txt
作者: hfxiang    时间: 2022-10-11 08:40

如不介意用第3方工具,可用gawk(http://bcn.bathome.net/tool/4.1.0/gawk.exe),指令如下:
  1. gawk "A;A=0;/Did You Know\?/{A=1}" test.txt
复制代码

作者: qixiaobin0715    时间: 2022-10-11 08:52

  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
复制代码

作者: Batcher    时间: 2022-10-11 09:02

回复 1# ronger28


    建议把原始文件上传到网盘,方便大家测试代码。
作者: qixiaobin0715    时间: 2022-10-11 09:05

3楼有链接。
作者: xp3000    时间: 2022-10-11 09:13

本帖最后由 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"
作者: ronger28    时间: 2022-10-11 21:55

回复 10# xp3000

完美解决问题,看着5000多条数据像跑火车一样,非常开心!非常感谢!向您学习!




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