简体   繁体   中英

Grepping out data from a returned wget

I am writing a bash script to use with badips.com

This command:

wget https://www.badips.com/get/key -qO -

Will return something like this:

{"err":"","suc":"new key 5f72253b673eb49fc64dd34439531b5cca05327f has been set.","key":"5f72253b673eb49fc64dd34439531b5cca05327f"}

Or like this:

{"err":"","suc":"Your Key was already present! To overwrite, see http:\/\/www.badips.com\/apidoc.","key":"5f72253b673eb49fc64dd34439531b5cca05327f"}

I need to parse the key value out ( 5f72253b673eb49fc64dd34439531b5cca05327f ) into a variable in the script. I would prefer to use grep to do it but can't get it right.

Instead of parsing with some grep , you have the perfect tool for this: jq .

See:

jq '.key' file

or

.... your_commands .... | jq '.key'

will return

"5f72253b673eb49fc64dd34439531b5cca05327f"

See another example, for example to get the suc attribute:

$ cat a
{"err":"","suc":"new key 5f72253b673eb49fc64dd34439531b5cca05327f has been set.","key":"5f72253b673eb49fc64dd34439531b5cca05327f"}
{"err":"","suc":"Your Key was already present! To overwrite, see http:\/\/www.badips.com\/apidoc.","key":"5f72253b673eb49fc64dd34439531b5cca05327f"}
$ jq '.suc' a
"new key 5f72253b673eb49fc64dd34439531b5cca05327f has been set."
"Your Key was already present! To overwrite, see http://www.badips.com/apidoc."

您可以尝试以下grep命令,

grep -oP '"key":"\K[^"]*(?=")' file

Using :

wget https://www.badips.com/get/key -qO - |
    perl -MJSON -MFile::Slurp=slurp -le '
        my $s = slurp "/dev/stdin";
        my $d = JSON->new->decode($s);
        print $d->{key}
'

Not as strong as precedent one, but that don't require to install new modules, a stock perl can do it :

     wget https://www.badips.com/get/key -qO - | 
         perl -lne 'print $& if /"key":"\K[[:xdigit:]]+/'

awk keeps it simple

wget ... - | awk -F:  '{split($NF,k,"\"");print k[2]}'
  1. the field separator is : ;
  2. the key is always in the last field, in awk this field is accessed using $NF (Number of Fields);
  3. the split function splits $NF and puts the pieces in array k , according to separator "\\"" that is just a single double quote character;
  4. the second field of the k array is what you want.

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