【问题标题】:Appending query result from a shell script to a csv file将来自 shell 脚本的查询结果附加到 csv 文件
【发布时间】:2016-07-27 11:28:30
【问题描述】:

所以我正在查询以获取我的 gps 位置。 shell脚本命令是这样的:

curl -s http://whatismycountry.com/ | sed -n 's/.*Coordinates \(.*\)<.*/\1/p'

然后将坐标保存到我写的 .csv 文件中:

curl -s http://whatismycountry.com/ | sed -n 's/.*Coordinates \(.*\)<.*/\1/p' | sed -e 's/.*\[\([^ ]*\) \([^]]*\)\].*/\1,\2/' > cordinates.csv

这给了我一个带有坐标的 csv 文件。 the image of the .csv file pattern

现在查询处于无限循环中,其目的是每次查询时都应将新坐标保存到下面的下一个块中。

Something like this

如何在脚本中编写上一个命令中的正则表达式部分?

感谢您的大力帮助。完全是正则表达式的菜鸟。

【问题讨论】:

    标签: shell csv unix


    【解决方案1】:

    这与正则表达式无关。您应该使用 &gt;&gt; 进行文件追加,而不是 &gt; 每次都重写文件。

    所以你的命令会变成

    curl -s http://whatismycountry.com/ |\ 
    sed -n 's/.*Coordinates \(.*\)<.*/\1/p' |\ 
    sed -e 's/.*\[\([^ ]*\) \([^]]*\)\].*/\1,\2/' >> cordinates.csv
    

    【讨论】:

    • 谢谢..这是一个很好的收获..完全错过了..现在可以了
    猜你喜欢
    • 2019-05-14
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    • 2018-06-18
    • 1970-01-01
    • 1970-01-01
    • 2019-01-29
    • 2015-01-10
    相关资源
    最近更新 更多