code从shell脚本文件中解析键/值 [英] Code for parsing a key/value in in file from shell script

查看:159
本文介绍了code从shell脚本文件中解析键/值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我需要使用一个shell脚本键来查找值的文件。该文件看起来像:

I have a file that I need to look up a value by key using a shell script. The file looks like:

HereIsAKey This is the value

怎么可以这样做:

How can I do something like:

MyVar=Get HereIsAKey

,然后为MyVar应该等于这就是价值。关键没有空白和值应该是继键后空白的一切。

and then MyVar should equal "This is the value". The key has no whitespace and the value should be everything following whitespace after the key.

推荐答案

如果 HereIsAKey 是您的文件独特的,试试这个使用grep:

if HereIsAKey is unique in your file, try this with grep:

myVar=$(grep -Po "(?<=^HereIsAKey ).*" file)

这篇关于code从shell脚本文件中解析键/值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆