标题: [文本处理] 批处理如何提取关键词所在行及其上下几行内容? [打印本页]
作者: suchost 时间: 2015-10-27 16:22 标题: 批处理如何提取关键词所在行及其上下几行内容?
现有文本1.txt,内容如下- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_IGNORE_XML_PROLOG]
- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_IMAGING_USE_ART]
- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_INTERNET_SHELL_FOLDERS]
- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_LEGACY_DISPPARAMS]
- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_LEGACY_DLCONTROL_BEHAVIORS]
- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_LOCALMACHINE_LOCKDOWN]
- "MSOSYNC.EXE"=dword:00000001
- "MSOUC.EXE"=dword:00000001
- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_MAXCONNECTIONSPER1_0SERVER]
- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_MAXCONNECTIONSPERSERVER]
- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_MIME_HANDLING]
- "MSOSYNC.EXE"=dword:00000001
- "MSOUC.EXE"=dword:00000001
- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_MIME_SNIFFING]
- "MSOSYNC.EXE"=dword:00000001
- "MSOUC.EXE"=dword:00000001
- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_MSHTML_AUTOLOAD_IEFRAME]
- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_OBJECT_CACHING]
- "MSOSYNC.EXE"=dword:00000001
- "MSOUC.EXE"=dword:00000001
- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_PROTOCOL_LOCKDOWN]
- "MSOSYNC.EXE"=dword:00000001
- "MSOUC.EXE"=dword:00000001
- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_RELEASE_CALLBACK_ON_STOP_BINDING]
- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_RESTRICT_ABOUT_PROTOCOL_IE7]
- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_RESTRICT_ACTIVEXINSTALL]
- "MSOSYNC.EXE"=dword:00000001
- "MSOUC.EXE"=dword:00000001
- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_RESTRICT_FILEDOWNLOAD]
- "MSOSYNC.EXE"=dword:00000001
- "MSOUC.EXE"=dword:00000001
- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_RESTRICT_OBJECT_DATA_ATTRIBUTE]
- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_RESTRICT_RES_TO_LMZ]
- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_SAFE_BINDTOOBJECT]
- "MSOSYNC.EXE"=dword:00000001
- "MSOUC.EXE"=dword:00000001
- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_SECURITYBAND]
- "MSOSYNC.EXE"=dword:00000001
- "MSOUC.EXE"=dword:00000001
- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_SHIM_MSHELP_COMBINE]
- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_SHOW_APP_PROTOCOL_WARN_DIALOG]
- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_SSLUX]
- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_SUBDOWNLOAD_LOCKDOWN]
- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_UNC_SAVEDFILECHECK]
- "MSOSYNC.EXE"=dword:00000001
- "MSOUC.EXE"=dword:00000001
- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_USE_WINDOWEDSELECTCONTROL]
- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_VALIDATE_NAVIGATE_URL]
- "MSOSYNC.EXE"=dword:00000001
- "MSOUC.EXE"=dword:00000001
- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_VIEWLINKEDWEBOC_IS_UNSAFE]
- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_WEBOC_MOVESIZECHILD]
- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_WEBOC_POPUPMANAGEMENT]
- "MSOSYNC.EXE"=dword:00000001
- "MSOUC.EXE"=dword:00000001
- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_WINDOW_RESTRICTIONS]
- "MSOSYNC.EXE"=dword:00000001
- "MSOUC.EXE"=dword:00000001
- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_XSSFILTER]
- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_ZONE_ELEVATION]
- "MSOSYNC.EXE"=dword:00000001
- "MSOUC.EXE"=dword:00000001
- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components\7660AEC6D8E6CFE42A4E2B964A5DC9E7]
- "00005119110000000100000000F01FEC"="C:\\Program Files\\Microsoft Office\\Office15\\MSOUC.EXE"
- [HKEY_USERS\S-1-5-21-787889350-1610004461-349859190-500\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\RunMRU]
- "c"="regedit.exe /e MSOUC.EXE.reg\\1"
复制代码
1、现在想得到如下的结果:
[-HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_LOCALMACHINE_LOCKDOWN]
"MSOSYNC.EXE"=dword:00000001
"MSOUC.EXE"=dword:00000001
[-HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_MIME_HANDLING]
"MSOSYNC.EXE"=dword:00000001
"MSOUC.EXE"=dword:00000001
[-HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_MIME_SNIFFING]
"MSOSYNC.EXE"=dword:00000001
"MSOUC.EXE"=dword:00000001
[-HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_OBJECT_CACHING]
"MSOSYNC.EXE"=dword:00000001
"MSOUC.EXE"=dword:00000001
[-HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_PROTOCOL_LOCKDOWN]
"MSOSYNC.EXE"=dword:00000001
"MSOUC.EXE"=dword:00000001
[-HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_RESTRICT_ACTIVEXINSTALL]
"MSOSYNC.EXE"=dword:00000001
"MSOUC.EXE"=dword:00000001
[-HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_RESTRICT_FILEDOWNLOAD]
"MSOSYNC.EXE"=dword:00000001
"MSOUC.EXE"=dword:00000001
[-HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_SAFE_BINDTOOBJECT]
"MSOSYNC.EXE"=dword:00000001
"MSOUC.EXE"=dword:00000001
[-HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_SECURITYBAND]
"MSOSYNC.EXE"=dword:00000001
"MSOUC.EXE"=dword:00000001
[-HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_UNC_SAVEDFILECHECK]
"MSOSYNC.EXE"=dword:00000001
"MSOUC.EXE"=dword:00000001
[-HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_VALIDATE_NAVIGATE_URL]
"MSOSYNC.EXE"=dword:00000001
"MSOUC.EXE"=dword:00000001
[-HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_WEBOC_POPUPMANAGEMENT]
"MSOSYNC.EXE"=dword:00000001
"MSOUC.EXE"=dword:00000001
[-HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_WINDOW_RESTRICTIONS]
"MSOSYNC.EXE"=dword:00000001
"MSOUC.EXE"=dword:00000001
[-HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_ZONE_ELEVATION]
"MSOSYNC.EXE"=dword:00000001
"MSOUC.EXE"=dword:00000001
[-HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components\7660AEC6D8E6CFE42A4E2B964A5DC9E7]
"00005119110000000100000000F01FEC"="C:\\Program Files\\Microsoft Office\\Office15\\MSOUC.EXE"
[-HKEY_USERS\S-1-5-21-787889350-1610004461-349859190-500\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\RunMRU]
"c"="regedit.exe /e MSOUC.EXE.reg\\1"
先谢了!
作者: CrLf 时间: 2015-10-27 16:43
本帖最后由 CrLf 于 2015-10-27 16:46 编辑
有意思,回家写个第三方
不过你要以什么依据来定位呢?
作者: pcl_test 时间: 2015-10-27 16:49
本帖最后由 pcl_test 于 2015-10-27 17:05 编辑
- //&cls&cscript -nologo -e:jscript "%~f0"<"1.txt"&pause&exit
- var m=WScript.StdIn.ReadAll().match(/\[[^\]]+?\]\r?\n[^\[]*?MSOUC\.EXE[^\[]*/g)
- for (var i=0; i<m.length; i++) WSH.echo(m[i].replace(/\[/g,'[-'));
复制代码
作者: suchost 时间: 2015-10-27 16:50
本帖最后由 suchost 于 2015-10-27 16:52 编辑
回复 2# CrLf
不用第三方的行吗?关键词为MSOUC.EXE
作者: suchost 时间: 2015-10-27 17:54
本帖最后由 suchost 于 2015-10-27 18:10 编辑
回复 3# pcl_test
版主很棒哦,但能根据关键词来提取关键词所在行及相应的[]吗?比如关键词为MSOUC,得到的结果为:
[-HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_ZONE_ELEVATION]
"MSOUC.EXE"=dword:00000001
这样呢?
作者: pcl_test 时间: 2015-10-27 18:26
本帖最后由 pcl_test 于 2015-10-27 18:28 编辑
回复 5# suchost - m[i].replace(/(\[)([^\]]+?\]\r?\n)[\s\S]*?(.*?MSOUC\.EXE.*)/,'$1-$2$3')
复制代码
作者: WHY 时间: 2015-10-27 18:44
纯P- @echo off & setlocal enabledelayedexpansion
- for /f "delims=" %%i in (1.txt) do (
- set "s=%%i"
- if "!s:~0,1!!s:~-1!" == "[]" (
- set "s1=!s:[=[-!"
- ) else (
- if not "!s:"MSOUC.EXE"=!" == "!s!" echo;!s1!&echo;!s!
- )
- )
- pause
复制代码
作者: 依山居 时间: 2015-10-27 19:12
不太理解为什么要从这个文件取。
直接从注册表直接读不行?
作者: suchost 时间: 2015-10-27 20:16
回复 8# 依山居
直接读取更好,请赐教啊!
作者: suchost 时间: 2015-10-27 20:27
回复 6# pcl_test
这行代码怎么用呀?
作者: suchost 时间: 2015-10-27 20:28
本帖最后由 suchost 于 2015-10-27 20:52 编辑
回复 7# WHY
怎么输出到文件呀?还有一行没显示出来
作者: Batcher 时间: 2015-10-27 20:43
回复 11# suchost
@echo off & setlocal enabledelayedexpansion
(for /f "delims=" %%i in (1.txt) do (
set "s=%%i"
if "!s:~0,1!!s:~-1!" == "[]" (
set "s1=!s:[=[-!"
) else (
if not "!s:"MSOUC.EXE"=!" == "!s!" echo;!s1!&echo;!s!
)
))>2.txt
pause
作者: Batcher 时间: 2015-10-27 20:44
回复 10# suchost - //&cls&cscript -nologo -e:jscript "%~f0"<"1.txt"&pause&exit
- var m=WScript.StdIn.ReadAll().match(/\[[^\]]+?\]\r?\n[^\[]*?MSOUC\.EXE[^\[]*/g)
- for (var i=0; i<m.length; i++) WSH.echo(m[i].replace(/(\[)([^\]]+?\]\r?\n)[\s\S]*?(.*?MSOUC\.EXE.*)/,'$1-$2$3'));
复制代码
整段代码保存为 test.bat 双击执行
作者: Batcher 时间: 2015-10-27 20:47
回复 9# suchost
你的 1.txt 是执行哪个命令得到的结果?发出来看看吧
作者: /zhqsystem/zhq 时间: 2015-10-27 21:00
我只想问楼主真的了解注册表吗?
作者: suchost 时间: 2015-10-27 21:00
本帖最后由 suchost 于 2015-10-27 21:59 编辑
回复 14# Batcher
手工导出,哈哈,一个有用的导出注册表代码:- @set @n=0;/* & echo off & setlocal enabledelayedexpansion
- md "reg" 2>nul
- pushd "reg\"
- (for %%a in (HKLM HKCU HKCR HKU HKCC) do (
- set "n="
- for /f "delims=" %%b in ('reg query "%%a" /s /f "txt" /k') do (
- set /a n+=1
- reg export "%%b" %%a_!n!.reg
- )
- )) 2>nul
- dir /b *.reg | cscript -nologo -e:jscript "%~0"
- start "" ".\"
- pause & exit/b & rem */
-
- fso = new ActiveXObject("Scripting.FileSystemObject");
- while (!WScript.StdIn.AtEndOfstream) {
- file = WScript.StdIn.ReadLine();
- f = fso.OpenTextFile(file, 1, false, -1);
- txt= f.Readall();
- f.Close();
- txt= /[\s\S]*?^\[[\s\S]*?(?=^\[)/m.exec(txt + "\r\n[")[0];
- fso.OpenTextFile(file, 2, true, -1).write(txt);
- }
复制代码
注意改txt
作者: suchost 时间: 2015-10-27 21:01
回复 15# /zhqsystem/zhq
不了解
作者: suchost 时间: 2015-10-27 21:04
回复 6# pcl_test
执行报错
作者: /zhqsystem/zhq 时间: 2015-10-27 21:07
回复 17# suchost r
如果不了解不要删除某个主项,这样会吧其他文件破坏,删除对应键值和关联即可[当这个主项全部为一个软件的注册表项时才可以全部删除],如果将主项全部删除会出现连锁反应或者未知的错误导致系统不能正常开机,最好保留原始注册表文件备用
作者: suchost 时间: 2015-10-27 21:10
回复 19# /zhqsystem/zhq
这个不用担心的,秒还原
作者: pcl_test 时间: 2015-10-27 21:22
本帖最后由 pcl_test 于 2015-10-27 21:23 编辑
回复 18# suchost
报什么错?肯定没改对,把你改的代码发出来,报错也截图出来
作者: /zhqsystem/zhq 时间: 2015-10-27 21:29
回复 20# suchost
对于秒还原我就不回什么了直接给你改变完的代码- @echo off
- (echo Windows Registry Editor Version 5.00
- echo/)>"%~dp0\1_new.reg"
- (for /f "delims=[" %%i in ('type "%~dp0\1.txt"^|findstr/i "hkey_"') do (
- echo=[-%%i
- echo/
- ))>>"%~dp0\1_new.reg"
- pause
复制代码
作者: suchost 时间: 2015-10-27 21:55
回复 21# pcl_test
再次测试,发现没错,感谢大家的热情帮助!
作者: CrLf 时间: 2015-10-30 07:18
给这个需求量身定做的:
http://www.bathome.net/thread-37861-1-1.html- cscript -nologo context.js -iegoc -f /^\[/ 'msouc.exe' -t +0 Z:\a.txt
复制代码
- context.exe -iegoc -f /^\[/ 'msouc.exe' -t +0 Z:\a.txt
复制代码
- ncontext.exe -iegoc -f /^\[/ 'msouc.exe' -t +0 Z:\a.txt
复制代码
需要的话还可以用 --color 开关高亮显示
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |