Home > Net >  Extract all similar words in a list in shell script
Extract all similar words in a list in shell script

Time:01-22

I have a list like below:

list="-list mail-username:123 --list mail-password:xyz --list url:https://www.google.com --list mail_username:123 --list mail_password:xyz --list mail_username:123 --list user_password:xyz"

I want to extract the values of all passwords and store it in a variable.

I tried using sed but couldn't get it.

CodePudding user response:

You could try:

pwds="$(echo "$list" | sed 's/[[:space:]]\{1,\}/\n/g' | sed '/password/!d;s/^[^:]*password:\(.*\)$/\1/g')"

This will not work for passphrases with spaces, though.

CodePudding user response:

This will include the trailing newline in the final password, but since you seem to be playing fast and loose with whitespace, that shouldn't be a problem. (Or, at least, it's not more of a problem than the other issues that will crop up from being cavalier about whitespace!)

echo "$list" | awk '/password/{print $2}' RS=' '  FS=:
  •  Tags:  
  • Related