Reputation: 19
I am trying to write a awk
script where i am capturing a API response. The response looks like below.
id:0tesss status:ACTIVE created:2016-12-06T13:24:50.000Z activated:2016-12-06T13:26:06.000Z statusChanged:2016-12-06T13:26:06.000Z lastLogin:2017-06-13T06:32:41.000Z lastUpdated:2017-06-13T06:32:58.000Z passwordChanged:null profile:lastName:tack profile:secondEmail:null profile:mobilePhone:null profile:email:[email protected] profile:login:[email protected] profile:firstName:Karry profile:displayName:Karry tack credentials:recovery_question:{question:Who's a ?}
In the above i just want to print profile:login
value which is below.
[email protected]
How can i print this. This response can vary for different users. So i have to filter based on profile.login
and print the next to a csv file. Appreciate your help.
Upvotes: 0
Views: 38
Reputation: 5298
You can do it with this command (using tr
and awk
):
tr ' ' '\n' | awk -F: '/profile:login/{print $NF}'
Sample:
AMD$ echo "id:0tesss status:ACTIVE created:2016-12-06T13:24:50.000Z activated:2016-12-06T13:26:06.000Z statusChanged:2016-12-06T13:26:06.000Z lastLogin:2017-06-13T06:32:41.000Z lastUpdated:2017-06-13T06:32:58.000Z passwordChanged:null profile:lastName:tack profile:secondEmail:null profile:mobilePhone:null profile:email:[email protected] profile:login:[email protected] profile:firstName:Karry profile:displayName:Karry tack credentials:recovery_question:{question:Who's a ?}" | tr ' ' '\n' | awk -F: '/profile:login/{print $NF}'
[email protected]
Or with just awk
:
awk -vRS=' ' '/profile:login/{sub(/.*:/, ""); print}'
Sample:
AMD$ echo "id:0tesss status:ACTIVE created:2016-12-06T13:24:50.000Z activated:2016-12-06T13:26:06.000Z statusChanged:2016-12-06T13:26:06.000Z lastLogin:2017-06-13T06:32:41.000Z lastUpdated:2017-06-13T06:32:58.000Z passwordChanged:null profile:lastName:tack profile:secondEmail:null profile:mobilePhone:null profile:email:[email protected] profile:login:[email protected] profile:firstName:Karry profile:displayName:Karry tack credentials:recovery_question:{question:Who's a ?}" | awk -vRS=' ' '/profile:login/{sub(/.*:/, ""); print}'
[email protected]
Upvotes: 1
Reputation: 4043
awk
can extract what you desired in the file.
$ awk '{match($0,/profile:login[^\ ]+/);print substr($0,RSTART+14,RLENGTH-14)}' file
[email protected]
Brief explanation,
match($0,/profile:login[^\ ]+/)
: find the pattern match the regex in the $0
, and store the start point in RSTART
, the length in RLENGTH
substr($0,RSTART+14,RLENGTH-14)}
: print the sub-string that omit the previous unwanted patternUpvotes: 1