Board logo

标题: [文本处理] 能否用findstr根据关键词提取连续几行内容? [打印本页]

作者: Tina0910    时间: 2017-5-12 21:13     标题: 能否用findstr根据关键词提取连续几行内容?

请问各位大侠,用findstr可以方便的根据关键词提取某一行内容。但是我想根据关键词,提取联系的几行内容,可以实现吗?
要求是这样的,需要从很多保存在本地的html中(1.html,2.html...)提取"Members of the public"及其后面的两行。
*********************
...
<tr>
<th>Type</th>
<th class='num'>Count</th>
<th class='num percent'>As %</th>
</tr>
<tr>
<td>Members of the public</td>
<td class='num'>2</td>
<td class='num'>100%</td>
</tr>
</table>
...

******************

可以实现吗?谢谢!
作者: Batcher    时间: 2017-5-14 20:07

findstr命令本身没有这样的参数
grep是可以的
  1. grep -A 2 "Members of the public" 1.html
复制代码
http://bbs.bathome.net/s/tool/index.html?key=grep
作者: taofan712    时间: 2017-5-15 22:55

可以用findstr找到关键字的行号,再找行号+1,+2,+3。
作者: taofan712    时间: 2017-5-15 23:16

  1. @echo off
  2. setlocal enabledelayedexpansion
  3. set fn=html_tmp.txt
  4. cd.>%fn%
  5. for %%a in (*.html) do (
  6.   for /f "tokens=1,2 delims=:" %%b in ('findstr /n "Members of the public" "%%~a"') do (
  7.     set /a num=%%b
  8.     echo;________%%~nxa___>>%fn%
  9.     echo;%%c>>%fn%
  10.   )
  11.   set /a num2=num+1
  12.   for /f "tokens=1,2 delims=:" %%d in ('findstr /b /n .* "%%~a"^|findstr /b !num2!') do echo;%%e>>%fn%
  13.   set /a num3=num2+1
  14.   for /f "tokens=1,2 delims=:" %%f in ('findstr /b /n .* "%%~a"^|findstr /b !num3!') do echo;%%g>>%fn%
  15. )
  16. echo;over&pause>nul&exit /b
复制代码
获取包括关键字的连续3行,只是效率极低,极!低!
作者: codegay    时间: 2017-5-16 00:56

可以用xmlstartlet xmlint 这些东西。需要学下xml xpath




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