Board logo

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

作者: yuanyannian    时间: 2014-7-24 21:09     标题: (已解决)批处理能处理文本中的段落,比如整个段落截取,或删除呢?

本帖最后由 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]” 及其所有内容,
该如何处理呢?
作者: apang    时间: 2014-7-25 00:59

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

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

回复 2# apang


    好用,非常感谢!

   这好像是 VBS,bat 可以吗?
作者: apang    时间: 2014-7-25 09:10

回复 3# yuanyannian


    截取:
  1. @echo off & setlocal enabledelayedexpansion
  2. set "file=HIVESY.INF"
  3. set "key=[AddReg]"
  4. (for /f "delims=" %%a in ('findstr /n .* "%file%"') do (
  5.     set "str=%%a"
  6.     set "str=!str:*:=!"
  7.     if "!str:%key%=!" == "" (
  8.         set flag=1
  9.     ) else if "!str:~,1!" == "[" set "flag="
  10.     if defined flag echo,!str!
  11. ))>"New_%file%"
  12. pause
复制代码
删除:
第10行改成:
  1.     if not defined flag echo,!str!
复制代码

作者: yuanyannian    时间: 2014-7-25 19:54

回复 4# apang

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




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