【问题标题】:how to get a string using shell script commands? [duplicate]如何使用 shell 脚本命令获取字符串? [复制]
【发布时间】:2019-11-02 10:36:15
【问题描述】:

我有一个包含以下数据的文件:

__NV__: 1
name: "MEP-UI-CUSTOMER-INFO-TXT"
desc: "MEP Customer Name Information"
fpath: "mep/frontend/ui/primecast/assets/i18n/custom-info.txt"
fdata: "telestra"

我想获取 fdata (telestra) 的值。 如何使用 shell 脚本实现?

【问题讨论】:

    标签: bash sh


    【解决方案1】:

    您可以使用awkgrepsed 来执行此任务。

    这里有几个例子。

    注意:提供的示例数据已存储在名为sample.txt的文件中

    Awk

    # The NF means the number of fields,
    # it is used to print the last field
    awk '/fdata/ { print $NF }' sample.txt
    "telestra"
    

    Grep

    # Note that this one returns both
    # the field label, as well as the value
    grep fdata sample.txt
    fdata: "telestra"
    

    Grep + Sed

    # The results of grep are piped into
    # sed, which then removes the field name
    grep fdata sample.txt | sed 's/fdata: //g'
    "telestra"
    

    【讨论】:

      猜你喜欢
      • 2022-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-17
      • 2019-09-05
      相关资源
      最近更新 更多