Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use sed, grep, or awk without perl to replicate positive lookbehind

I need to extract RJ3fadfiasdUYBxF6z from:

 {"user":{"id":1325135,"uuid":"134513451","email":"[email protected]","joined_at":"2012-01-01T013:511:124.000Z","username":"testicl","title":"testli","thumb":"https://plex.tv/user/avatar?c=","hasPassword":true,"authToken":"RJ3fadfiasdUYBxF6z","authentication_token":"RJ3fadfiasdUYBxF6z","subscription":{"active":false,"status":"Inactive","plan":null,"features":["adaptive_bitrate","collections","photos-metadata-edition","radio","photos-favorites","federated-auth","Android - PiP","publishing_platform","news","kevin-bacon","client-radio-stations","TREBLE-show-features","web_server_dashboard","conan_redirect_qa","conan_redirect_alpha","conan_redirect_beta","transcoder_cache"]},"roles":{"roles":[]},"entitlements":[],"confirmedAt":"2012-01-01T13:31:31.000Z","forumId":23573,"rememberMe":false}}

regex with PCRP works great

(?<=authToken\":\")(\w+)

How can I extract RJ3fadfiasdUYBxF6z using either awk, sed, or grep without positive lookbehind? I don't have perl support.
I'm doing this out of a bash script on an openwrt router.

like image 674
stinkybummer Avatar asked Mar 09 '19 04:03

stinkybummer


3 Answers

May be you could install jq and use it?

jq .user.authToken < a.json
"RJ3fadfiasdUYBxF6z"
like image 179
Fedor Dikarev Avatar answered Nov 15 '22 11:11

Fedor Dikarev


You can use the following sed command:

sed 's/.*"authToken":"\([^"]*\)".*/\1/' file
like image 36
blhsing Avatar answered Nov 15 '22 11:11

blhsing


It sounds like grep is fair game

grep -Po '(?<=authToken\":\")(\w+)' file

Note: This needs an extra library, libpcre, for grep's native PCRE support.

like image 37
zdim Avatar answered Nov 15 '22 11:11

zdim