shell脚本解析数据变量,然后更新数据库 [英] shell script parsing data to variables then update a database

查看:143
本文介绍了shell脚本解析数据变量,然后更新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的网络所发送的数据在HTML页面上的设备。我需要能够从页面收集数据并将其插入到我的数据库,所以我可以有历史参照数据。我可以用下面的命令来检索网站等领域。结果返回由换行分隔的三个数字。

我想运行此作为一个Linux cron作业,所以我想用一个Linux脚本:


  1. 保存数据(数字)变量

  2. 连接到我的数据库,并在表
  3. 插入数据

我的命令行来检索信息如下。这从设备解析出的数据的伟大工程:

卷曲-s http://local_device.com/dtm.html?address=C5:0 |的grep -Po'[0-9] +(= [^ 0-9] +(C 5:2'; | C5:6≤| C5:13))'


解决方案
参见下面的一个参考,因为它们是数字,我没有说出来了:

 设置 -  $(卷曲-s http://local_device.com/dtm.html?address=C5:0 | grep的-Po[0-9] +(? = [^ 0-9] +(C 5:2'; | C5:6≤| C 5:13))')
C5_2 = $ 1
C5_6 = $ 16
C5_13 = $ 3
MySQL的-e
   INSERT INTO db.table VALUES($ C5_2,$ C5_6,$ C5_13)

I have a device on my network that is posting data to an html page. I need to be able to collect the data from the page and insert it into my database so I can have historical reference to the data. I am able to use the following command to retrieve the fields from the site. The result returns three numbers separated by a line break.

I would like to run this as a linux cron job, so I would like to use a linux script to:

  1. save the data (numbers) to variables
  2. connect to my database and insert the data in a table

My command line to retrieves the information is below. This works great for parsing out the data from the device:

 curl -s http://local_device.com/dtm.html?address=C5:0 | grep -Po '[0-9]+(?=[^0-9]+(C5:2<|C5:6<|C5:13))'

解决方案

see below for a reference, since they are numbers, I did not quote them:

set -- $(curl -s http://local_device.com/dtm.html?address=C5:0 | grep -Po '[0-9]+(?=[^0-9]+(C5:2<|C5:6<|C5:13))')
C5_2=$1
C5_6=$2
C5_13=$3
mysql -e "
   INSERT INTO db.table VALUES ($C5_2, $C5_6, $C5_13) 
"

这篇关于shell脚本解析数据变量,然后更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆