如何使用Shell脚本命令获取字符串?

时间:2019-06-19 16:05:38

标签: bash sh

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

__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脚本来实现?

1 个答案:

答案 0 :(得分:3)

您可以为此任务使用awkgrepsed

以下是一些示例。

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

# 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"