【问题标题】:Extract text from command line script从命令行脚本中提取文本
【发布时间】:2019-04-14 13:47:59
【问题描述】:

我有一个显示我需要的数据的挖掘应用程序,但该应用程序没有 api 来获取它。如何提取字符串以使用 powershell 或等效工具解析数据?

数据每秒下降一行,如下所示,

ID(grabbed from board) hash:(label) hashrate(variable) errors:(label) #(variable) temp(variable) volts(variable) solutions:(label) #(variable) shares:(label) #(variable)

例子:

ABC1000234 哈希:9.8Gh/s 错误:0.000% 26.3C 0.74V 解决方案:539/539 分享:33

我需要哈希率、温度和伏特,甚至更好的方法来将每个字符串发送到一个端口,我可以监听一个像“字符串”这样的 url。如果我可以让字符串发布到 4068 等端口。然后我可以使用 powershell 和 netcat 来监听http://127.0.0.1:4068 上的端口。

这是我打算为 powershell 做的事情:

$serveraddress = '127.0.0.1' 

$serverport = '4068'

$threadinfo = echo 'strings' | nc $serveraddress $serverport

$mineridstring = $stringsinfo.Split(';')[1] $minderid =
$mineridstring.Split('=')[0]

$hashstring = $stringsinfo.Split(';')[2] $hash =
$hashstring.Split('=')[1]

$tempstring = $stringsinfo.Split(';')[4] $tempc =
$tempstring.Split('=')[0]

$voltstring = $stringsinfo.Split(';')[5] $volts =
$voltsstring.Split('=')[0]

Invoke-RestMethod -Uri https://www.rigmanager.xyz/rig.php -Method Post `
-Body @{minerid = $minerid; hashrate = $hashrate; tempc = $temp; $volts = $volts} -UseBasicParsing

【问题讨论】:

  • 感谢您愿意不断改进您的问题,但从抽象示例中仍然很难理解确切的要求是什么。尝试发布具体的、有代表性的样本,并补充格式说明。一般来说,争取MCVE (Minimal, Complete, and Verifiable Example) 或尽可能接近。
  • 请在问题中举个例子。是吗??? ID 237 hash:123 errors:#456...?
  • 添加了更多背景和示例。抱歉信息不足。我错过了什么吗?
  • 这种理解是否正确:您有一个程序(“挖矿应用程序”)。该程序正在产生输出。输出到它的控制台。您想找到一种方法来捕获此输出(可能使用 PowerShell),并对其进行解析以拉回相关值(例如温度、电压)。您不能对程序本身进行编码;所以你需要一种从控制台输出中提取数据的方法。
  • 如果我的理解是正确的,你可能想看看这个:stackoverflow.com/a/8762068/361842

标签: shell powershell cmd terminal


【解决方案1】:

将它们推送到消息队列,然后您可以为任意数量的用户/应用程序订阅该流。

查看 Apache Kafka 或 AWS、IBM Cloud、GCP 等上的任何基于云的等价物。

解析您的字符串是正则表达式可以处理的事情,但除非您需要为查询/搜索编制索引的数据,否则您可以将其推送给最终用户/应用程序,并为他们提供整个消息。

【讨论】:

  • 如何将它们推送到消息队列?
【解决方案2】:

一个简单的方法是在正则表达式中使用命名捕获。

PS C:\src\t> type  exttext.ps1
$s = 'ABC1000234 hash: 9.8Gh/s errors: 0.000% 26.3C 0.74V solutions: 539/539 shares: 33'

$doesit = $s -match '^(?<id>.*) hash: (?<hashrate>.*) errors: (?<errors>[0-9.]+%) (?<temp>.*) (?<volts>.*) solutions: .* shares: \d+$'

$Matches.id
$Matches.hashrate
$Matches.errors
$Matches.temp
$Matches.volts

PS C:\src\t> .\exttext.ps1
ABC1000234
9.8Gh/s
0.000%
26.3C
0.74V

【讨论】:

    猜你喜欢
    • 2015-06-02
    • 2016-04-24
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    • 2013-08-29
    相关资源
    最近更新 更多