首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用shell脚本命令获取字符串?

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

Stack Overflow用户
提问于 2019-06-19 16:05:38
回答 1查看 32关注 0票数 0

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

代码语言:javascript
运行
复制
__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脚本实现?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-19 16:18:36

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

以下是几个例子。

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

Awk

代码语言:javascript
运行
复制
# The NF means the number of fields,
# it is used to print the last field
awk '/fdata/ { print $NF }' sample.txt
"telestra"

Grep

代码语言:javascript
运行
复制
# Note that this one returns both
# the field label, as well as the value
grep fdata sample.txt
fdata: "telestra"

Grep + Sed

代码语言:javascript
运行
复制
# The results of grep are piped into
# sed, which then removes the field name
grep fdata sample.txt | sed 's/fdata: //g'
"telestra"
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56671876

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档