1

I'm getting string from some service something like this,

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

I want to extract encodedHeader value which

SCBjYjA5MTEtNTFjZi00ZTBjLWFmN2UtMzFhDTYzYzA3EjFk

I tried sed with version I know but no luck. any help on this

3 Answers 3

2

If you have GNU awk then

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

Test Results:

$ 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
Sign up to request clarification or add additional context in comments.

1 Comment

Beginner I would recommend that you use @3161993 solution +1 :) . You don't have to worry about he number of fields being changed.
0

Here is one way to do it using AWK.

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

And

~$ echo $myline  | awk -F[:,] '{print $9 }'

Ouputs:

SCBjYjA5MTEtNTFjZi00ZTBjLWFmN2UtMzFhDTYzYzA3EjFk

[:,] Match a single character present in the list.

,: are the field separators and the field you want is number 9.

1 Comment

You can always accept as an answer if it does what you wanted :)
0
awk -F\" '{print $16}' file

SCBjYjA5MTEtNTFjZi00ZTBjLWFmN2UtMzFhDTYzYzA3EjFk

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.