Board logo

标题: [文本处理] 批处理替换文本指定字符串问题 [打印本页]

作者: heshibin    时间: 2015-11-8 20:59     标题: 批处理替换文本指定字符串问题

{"AlignCell":true,"AutoSort":false,"BlackTagIconList":[{"Type":"sysIcon","name":"呼叫服务.lnk"},{"Type":"sysIcon","name":"商品超市.lnk"}],"IconCellid":"","IconList":[{"Type":"sysIcon","bottom":440,"left":10,"name":"英雄联盟TGP.lnk","right":110,"top":340},{"Type":"sysIcon","bottom":240,"left":10,"name":"网吧专用.lnk","right":110,"top":140},{"Type":"sysIcon","bottom":340,"left":10,"name":"腾讯QQ.lnk","right":110,"top":240},{"Type":"sysIcon","bottom":140,"left":10,"name":"::{20D04FE0-3AEA-1069-A2D8-08002B30309D}","right":110,"top":40},{"Type":"sysIcon","bottom":540,"left":10,"name":"侠盗列车5.lnk","right":110,"top":440},{"Type":"sysIcon","bottom":640,"left":10,"name":"挂机锁.lnk","right":110,"top":540},{"Type":"sysIcon","bottom":740,"left":10,"name":"用户中心.lnk","right":110,"top":640},{"Type":"sysIcon","bottom":840,"left":10,"name":"自助结账.lnk","right":110,"top":740}],"IconSize":2,"WallpaperType":0,"WhiteIconList":[{"Type":"sysIcon","name":"::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"},{"Type":"sysIcon","name":"侠盗列车5.lnk"},{"Type":"sysIcon","name":"用户中心.lnk"},{"Type":"sysIcon","name":"网吧专用.lnk"},{"Type":"sysIcon","name":"自助结账.lnk"},{"Type":"sysIcon","name":"英雄联盟TGP.lnk"}]}
以上是批处理一个文本内容 我的目的就是提取"WallpaperType":0 后面的0是可变的 无论怎么变都是要替换成"WallpaperType":0
作者: Batcher    时间: 2015-11-8 21:15

  1. sed -r "s/(\"WallpaperType\":)[^,]+,/\10,/" 1.txt > 2.txt
复制代码

作者: /zhqsystem/zhq    时间: 2015-11-9 00:52

终于编好了,楼主测试吧
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. for /f "delims=" %%i in ('type "%~dp0\1.txt"') do (
  4. for %%j in (%%i) do (
  5.   for /f "tokens=1,* delims=:" %%k in ("%%j") do (
  6.    if /i "%%~k"=="WallpaperType" (
  7.     set "name=!name!,"%%~k":0"
  8.    ) else (
  9.     if defined name set "name=!name!,%%j"
  10.     if not defined name set "name=%%j"
  11.    )
  12.   )
  13. )
  14. )
  15. (echo=!name!)>2.txt
  16. pause
复制代码

作者: CrLf    时间: 2015-11-9 01:07

既然是 JSON,那用 JS 最方便了的:
  1. <!-- :
  2. @echo off
  3. mshta "%~f0" <转换前.txt >转换后.txt
  4. exit
  5. -->
  6. <script src=http://www.bathome.net/lib/diy/hide.js></script>
  7. <script src=http://www.bathome.net/lib/diy/WSH.js></script>
  8. <script src=http://www.bathome.net/lib/json2.js></script>
  9. <script>
  10. var json = JSON.parse(WSH.StdIn.ReadAll())
  11. json.WallpaperType = 0
  12. WSH.Echo(JSON.stringify(json))
  13. </script>
复制代码

作者: 依山居    时间: 2015-11-9 02:47

  1. @echo off
  2. ::依山居 1:54 2015/11/9
  3. ::fr修改WallpaperType
  4. ::http://www.bathome.net/thread-38056-1-1.html
  5. ::fr是一个完整支持正则表达式查找替换的命令行工具,
  6. ::作者网站:http://baiy.cn/utils/fr/index.htm
  7. ::去掉-stdout参数则直接修改原文件
  8. ::直接修改原文件
  9. fr -r:"\"WallpaperType\":\d+," -t:"\"WallpaperType\":1," -trc x.json
  10. ::修改的内容,原文件不修改,重定向输出到新文件。
  11. fr -r:"\"WallpaperType\":\d+," -t:"\"WallpaperType\":1," -stdout -trc x.json>test.txt
  12. pause
复制代码





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