[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[文本处理] 求助批处理提取指定行的指定字段并输出成变量

不能插图也不能上传附件。。。。
如下是TXT 内容
Description,Value
Battery Name,02DL008
Manufacture Name,SMP
Serial Number, 2669
Manufacture Date,
Power State,"Charging, AC Power"
Current Capacity (in %),67.6%
Current Capacity Value,"29,920 mWh"
Full Charged Capacity,"44,260 mWh"
Designed Capacity,"50,450 mWh"
Battery Health,87.7%
Voltage,"12,868 millivolts "
Charge/Discharge Rate ,"32,852 milliwatts "
Chemistry ,LiP
Low Battery Capacity (1),200 mWh
Low Battery Capacity (2),"2,213 mWh"
Critical Bias,
Number of charge/discharge cycles,184
Battery Temperature,
Remaining battery time for the current activity (Estimated),
Full battery time for the current activity (Estimated),
Remaining time for charging the battery (Estimated),
Total  time for charging the battery (Estimated),

需要提取第11行Battery Health,87.7%和18行Number of charge/discharge cycles,184 的第二字节 并输出成变量,这个是做电池健康度监控使用的,Battery Health 必须大于95%,而充放电次数小于等于3, 但是我目前只能
  1. for /f "skip=10 delims=, tokens=2 " %%i in (scomma.txt) do echo %%i
复制代码
只能显示10行和之后的输出,百度了各种也成功不了,新手入门,难度太大,还请大佬不吝赐教!
小Z

链接:https://pan.baidu.com/s/1hUqCjhlsLnp44ETG_T-tzg
提取码:1234
文档上传了百度网盘
小Z

TOP

回复 1# smtcao008
改了一下,不知道是否满足要求
  1. sed -rn "s/^Battery Health,(.*)%$/\1/p;s#^Number of charge/discharge cycles,(.*)$#\1#p" scomma.txt
复制代码

TOP

回复 10# aloha20200628
严谨一点的话,最好加参数使用findstr,
  1. findstr /ibc:"Battery Health," /c:"Number of charge/discharge cycles," <scomma.txt
复制代码

TOP


用findstr可一次锚定两个字段(不能忽略大小写)... 正好示例文件中没有重复关键词
  1. for /f "tokens=2 delims=," %%s in ('findstr "Health, charge/discharge" scomma.txt') do (echo,%%s)
复制代码

TOP

回复 5# smtcao008
哦 可能环境问题 我这里运行没问题

TOP

回复 1# smtcao008


如果需要上传文件,可以用阿里云盘或百度网盘。

如果需要上传截图,可以找个图床,例如:
http://bbs.bathome.net/thread-60985-1-1.html
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

回复 6# hfxiang

谢谢!~~~
小Z

TOP

回复 1# smtcao008
第3方工具sed( http://bcn.bathome.net/tool/4.9/sed.exe )比较方便
  1. sed -n "/Battery Health/p;\#Number of charge/discharge cycles#p" scomma.txt
复制代码
1

评分人数

    • smtcao008: 结果是一整行,没用,只要数字技术 + 1

TOP

回复 3# terse


    这个运行提示 此处不该出现else   我研究下,感谢感谢!
小Z

TOP

回复 2# pd1


   学习了,谢谢
小Z

TOP

  1. @echo off
  2. for /f "skip=10 delims=, tokens=1,* " %%i in (scomma.txt) do (
  3.       if /i "%%i" == "Battery Health" (set "Health=%%j") else if /i "%%i" == "Number of charge/discharge cycles" set "cycles=%%j"
  4. )
  5. if defined Health echo(%Health%
  6. if defined cycles echo(%cycles%
  7. pause
复制代码
1

评分人数

    • smtcao008: 提示 else 报错,去掉百分号呢,有百分号不 ...技术 + 1

TOP

  1. for /f "tokens=2 delims=," %%i in ('type scomma.txt^|find "Battery Health"') do echo %%i
复制代码
1

评分人数

TOP

返回列表