Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell脚本字符串提取_Shell - Fatal编程技术网

Shell脚本字符串提取

Shell脚本字符串提取,shell,Shell,我从这样的服务中得到了线索 {"statusCode":200,"statusText":"OK","responseEntity":{"authMethod":"Bearer","encodedHeader":"SCBjYjA5MTEtNTFjZi00ZTBjLWFmN2UtMzFhDTYzYzA3EjFk","expiresDate":"2017-10-16T14:58:24.697Z"}} 我想提取encodedHeader值 SCBjYjA5MTEtNTFjZi00ZTBjLWFmN2

我从这样的服务中得到了线索

{"statusCode":200,"statusText":"OK","responseEntity":{"authMethod":"Bearer","encodedHeader":"SCBjYjA5MTEtNTFjZi00ZTBjLWFmN2UtMzFhDTYzYzA3EjFk","expiresDate":"2017-10-16T14:58:24.697Z"}}
我想提取encodedHeader值

SCBjYjA5MTEtNTFjZi00ZTBjLWFmN2UtMzFhDTYzYzA3EjFk
我用我知道的版本试过sed,但运气不好。
关于这方面的任何帮助

这里有一种使用
AWK
的方法

~$ myline={"statusCode":200,"statusText":"OK","responseEntity":{"authMethod":"Bearer","encodedHeader":"SCBjYjA5MTEtNTFjZi00ZTBjLWFmN2UtMzFhDTYzYzA3EjFk","expiresDate":"2017-10-16T14:58:24.697Z"}}

输出:

SCBjYjA5MTEtNTFjZi00ZTBjLWFmN2UtMzFhDTYzYzA3EjFk
[:,]匹配列表中的单个字符


,:
是字段分隔符,您想要的字段是数字9。

如果您有GNU
awk
,那么

awk 'match($0,/"encodedHeader":"([^"]*)"/,arr){print arr[1]}' <<<"$line"

awk'match($0,/“encodedHeader”):“([^”]*)”/,arr){print arr[1]}如果答案符合您的要求,您可以随时接受:)初学者我建议您使用@3161993解决方案+1:)。您不必担心字段数量会发生变化。
awk 'match($0,/"encodedHeader":"([^"]*)"/,arr){print arr[1]}' <<<"$line"
$ line='{"statusCode":200,"statusText":"OK","responseEntity":{"authMethod":"Bearer","encodedHeader":"SCBjYjA5MTEtNTFjZi00ZTBjLWFmN2UtMzFhDTYzYzA3EjFk","expiresDate":"2017-10-16T14:58:24.697Z"}}';
$ awk 'match($0,/"encodedHeader":"([^"]*)"/,arr){print arr[1]}' <<<"$line"
SCBjYjA5MTEtNTFjZi00ZTBjLWFmN2UtMzFhDTYzYzA3EjFk
awk -F\" '{print $16}' file

SCBjYjA5MTEtNTFjZi00ZTBjLWFmN2UtMzFhDTYzYzA3EjFk