【问题标题】:parsing stdout in shell script在 shell 脚本中解析标准输出
【发布时间】:2012-08-30 22:31:10
【问题描述】:

我最初的问题的继续:

shell script taking input from python program

所以我的 python 程序的标准输出存储在我的 shell 脚本的一个变量中。现在,我必须解析这个输出,以便获取最后 22 个字符的子字符串。这些是唯一对我重要的。不幸的是,没有办法真正识别这些最后的字符(“keyword=”等),这意味着我必须完全按照它们的位置来做到这一点

【问题讨论】:

  • 将有助于显示一些示例输出

标签: python shell stdout


【解决方案1】:

假设你使用bash,你可以使用子串扩展:

key=$(python ...)   # From  your previous question

suffix=${key: -22}  # The space between : and - is important

【讨论】:

    【解决方案2】:

    如果需要字符串的最后 N 个字符,可以使用简单的切片:

    >>> v="this is the stdout from my shell command stored in a variable"
    >>> v[-22:]
    'd stored in a variable'
    

    返回最后 22 个字符。

    【讨论】:

    • 这可能是最好的办法了,但最后22个字符要保留,所以v[-22:]更合适
    • @WilliamPursell 你是对的,我误读了这个问题。我已经相应地更新了答案。
    猜你喜欢
    • 1970-01-01
    • 2019-01-06
    • 1970-01-01
    • 2015-01-26
    • 2020-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-01
    相关资源
    最近更新 更多