简体   繁体   中英

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

I need to extract RJ3fadfiasdUYBxF6z from:

 {"user":{"id":1325135,"uuid":"134513451","email":"ansdfaha@aol.com","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.

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.

您可以使用以下sed命令:

sed 's/.*"authToken":"\([^"]*\)".*/\1/' file

May be you could install jq and use it?

jq .user.authToken < a.json
"RJ3fadfiasdUYBxF6z"

Some linux flavours ships with pcregrep where you can use the lookarounds..

Check this

$ pcregrep -o '(?<=authToken\":\")(\w+)' json.test 
RJ3fadfiasdUYBxF6z
$ 

Here is the version in my RHEL.

$ pcregrep --version
pcregrep version 7.8 2008-09-05
$ 

An awk solution (tested under openwrt ):

awk -F: -v RS=, '$1~/"authToken"/{gsub("\"","",$2);print $2}' file
RJ3fadfiasdUYBxF6z

Or for more precisely matching, use equal == :

awk -F: -v RS=, '$1=="\"authToken\""{gsub("\"","",$2);print $2}' file
RJ3fadfiasdUYBxF6z

$1=="\\"authToken\\"" functionally equals to $1~/^"authToken"$/ but is a lit bit faster.

Nested data structures must operated by tools that are aware of the data model (JSON in this case), otherwise there always will be present risks of false positives or unintentional failures. Now, jq answer was already given, here's an alternative using a walk-path unix tool for JSON: jtc :

bash $ jtc -w'<authToken>l' input.json
"RJ3fadfiasdUYBxF6z"
bash $ 

if you like to drop the quotes, introduce then -qq option.

PS> Disclosure: I'm the creator of the jtc - shell cli tool for JSON operations

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM