Search Replace multiple strings from command output (Perl or Sed)

0

Can someone suggest how can I do the follwing:

`

attributes:
     0x00000007 <blob>="Test"
     0x00000008 <blob>=<NULL>
     "acct"<blob>="abc@xyz.com"
     password: "mycrypticpassword"

`

If I want to extract abc@xyz.com and the password value and assign them to variables in a shell script, can it be done in perl or sed?

Thanks

perl
sed
pattern-matching
asked on Stack Overflow Apr 20, 2017 by (unknown user)

2 Answers

0

Using sed with backreference:

$ email=$(sed -n 's/[\t ]*"acct"<blob>="\([^"]*\)"/\1/p' file)
$ pass=$(sed -n 's/[\t ]*password: "\([^"]*\)"/\1/p' file)
$ echo $email
abc@xyz.com
$ echo $pass
mycrypticpassword
answered on Stack Overflow Apr 20, 2017 by SLePort
0

Extract the required value using Perl:

$ perl -nle 'print "$1$2" if /"acct"<blob>="(.*)"|password: "(.*)"/' file
abc@xyz.com
mycrypticpassword

grab the output using $(...) construct, and assign it to two variables using read EMAIL PASSWORD <<< ...:

$ read EMAIL PASSWORD <<< \
> $(perl -nle 'print "$1$2" if /"acct"<blob>="(.*)"|password: "(.*)"/' file)
$ echo $EMAIL
abc@xyz.com
$ echo $PASSWORD
mycrypticpassword
answered on Stack Overflow Apr 20, 2017 by Dmitry Egorov

User contributions licensed under CC BY-SA 3.0