返回列表 发帖

[文件操作] (已解决)批处理能处理文本中的段落,比如整个段落截取,或删除呢?

本帖最后由 yuanyannian 于 2014-7-25 19:55 编辑

批处理能处理文本中的某个段落,比如整个段落截取,或删除呢?
比如还是这个文本,其中如下小节:

[AddReg]
HKLM,"SYSTEM\CurrentControlSet\Control\FileSystem","Ntfs",0x00010003,0x00000001
HKLM,"SYSTEM\CurrentControlSet\Control\FontAssoc","ANSI(00)",0x00000002,"YES"
HKLM,"SYSTEM\CurrentControlSet\Control\FontAssoc","SYMBOL(02)",0x00000002,"NO"
HKLM,"SYSTEM\CurrentControlSet\Control\FontAssoc","OEM(FF)",0x00000002,"YES"
...

[DelReg]
HKLM,"SYSTEM\CurrentControlSet\Services\fsvga"
HKLM,"SYSTEM\CurrentControlSet\Control\Nls\Locale","0402"
HKLM,"SYSTEM\CurrentControlSet\Control\Nls\Locale","0403"
HKLM,"SYSTEM\CurrentControlSet\Control\Nls\Locale","0404"
HKLM,"SYSTEM\CurrentControlSet\Control\Nls\Locale","0405"
HKLM,"SYSTEM\CurrentControlSet\Control\Nls\Locale","0406"
HKLM,"SYSTEM\CurrentControlSet\Control\Nls\Locale","0407"
HKLM,"SYSTEM\CurrentControlSet\Control\Nls\Locale","0408"
HKLM,"SYSTEM\CurrentControlSet\Control\Nls\Locale","0409"
...

我想将节“[AddReg]” 其下的所有内容全部截取到一个新文本中,或者删除节“[DelReg]” 及其所有内容,
该如何处理呢?
76626yyn

本帖最后由 apang 于 2014-7-25 21:54 编辑

截取:
@set @n=0;/* & echo off
set "file=HIVESY.INF"
cscript -nologo -e:jscript "%~0"<"%file%">"New_%file%"
pause & exit/b & rem */
txt = WScript.StdIn.ReadAll();
re = /^\[AddReg][\s\S]+?(?=^\[)/img;
while ((ar = re.exec(txt)) != null) { WScript.Echo(ar) }COPY
删除:
@set @n=0;/* & echo off
set "file=HIVESY.INF"
cscript -nologo -e:jscript "%~0"<"%file%">"New_%file%"
pause & exit/b & rem */
txt = WScript.StdIn.ReadAll();
txt = txt.replace(/^\[Addreg][\s\S]+?(?=^\[)/img, "")
WScript.Echo(txt)COPY
如果[AddReg]节名连续出现,截取或删除[AddReg]节会出现遗漏
可能性虽然不大,还是改一下。
1

评分人数

    • CrLf: 原来 ] 可以不用转义技术 + 1

TOP

回复 2# apang


    好用,非常感谢!

   这好像是 VBS,bat 可以吗?
76626yyn

TOP

回复 3# yuanyannian


    截取:
@echo off & setlocal enabledelayedexpansion
set "file=HIVESY.INF"
set "key=[AddReg]"
(for /f "delims=" %%a in ('findstr /n .* "%file%"') do (
    set "str=%%a"
    set "str=!str:*:=!"
    if "!str:%key%=!" == "" (
        set flag=1
    ) else if "!str:~,1!" == "[" set "flag="
    if defined flag echo,!str!
))>"New_%file%"
pauseCOPY
删除:
第10行改成:
    if not defined flag echo,!str!COPY

TOP

回复 4# apang

非常感谢 apang 老师,解决了我的大问题,再次感谢!
76626yyn

TOP

返回列表