我有一个包含以下数据的文件:
__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脚本来实现?
答案 0 :(得分:3)
您可以为此任务使用awk
,grep
或sed
。
以下是一些示例。
注意:提供的示例数据已存储在名为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"