Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a properties file which has a password field (key value pair)

PASSWORD=NDhhHcsOBofXUdUzGw5B0Q==

I am trying to get the value in my shell script using the awk command

password=`awk -F "=" '{if($1 == "PASSWORD") print $2}' "$PASSWORD.ini"`

But it retrieves only NDhhHcsOBofXUdUzGw5B0Q.

I need the entire password (including the last two = characters).

share|improve this question
    
Are you base64 encoding passwords? –  Evan Teitelman Jul 29 '13 at 13:37
    
@Evan, yes, that's base64 and if you look at the unencoded value, it's a 16 byte value, so probably a MD5 hash of the password. –  Stephane Chazelas Jul 29 '13 at 14:27
add comment

7 Answers

password=$(sed -n 's/^PASSWORD=//p' < "$PASSWORD.ini")
share|improve this answer
add comment

cut would be the appropriate tool here if you already have the line extracted:

$ echo 'PASSWORD=NDhhHcsOBofXUdUzGw5B0Q==' | cut -d= -f2-
NDhhHcsOBofXUdUzGw5B0Q==

Here, you're taking fields from the 2nd to the end.

GNU grep with perl regular expressions would also work:

pw=$(grep -oP '^PASSWORD=\K.*' < filename)
share|improve this answer
add comment

Using awk:

password=$(awk 'sub(/^PASSWORD=/,"")' < "$PASSWORD.ini")
share|improve this answer
add comment

Using awk you are not limited to splitting on single characters. So you could split on D= for example or even PASSWORD=:

password=$(awk -F "D=" '{print $2}' $PASSWORD.ini)

or

password=$(awk -F "PASSWORD=" '{print $2}' $PASSWORD.ini)
share|improve this answer
add comment

There's several nice ways you can do this within awk depending on the particular approach that suits you:

Change your FS:

$ echo 'PASSWORD=NDhhHcsOBofXUdUzGw5B0Q==' | awk -FPASSWORD= '{print $2;}'
NDhhHcsOBofXUdUzGw5B0Q==

Or change your ORS (this is REALLY hacky):

$ echo 'PASSWORD=NDhhHcsOBofXUdUzGS="="; ORS="=";} {for (i=2;i<NF;i++) print $i}'
NDhhHcsOBofXUdUzGw5B0Q==

The robust and generic way:

$ echo 'PASSWORD=NDhhHcsOBofXUdUzGw5B0Q==' | awk 'BEGIN {FS="^[^=]+=";} {print $2;}'
NDhhHcsOBofXUdUzGw5B0Q==

or (in the case of parsing >1 line and finding the one for which you are searching):

$ echo -e 'foo=bar\nPASSWORD=NDhhHcsOBofXUdUzGw5B0Q==\nbaz=quux\n\n' | \
  awk 'BEGIN {FS="^[^=]+=";} /^PASSWORD=/ {print $2;}'
NDhhHcsOBofXUdUzGw5B0Q==
share|improve this answer
    
Isn't missing the awk call itself from the second code block? –  manatwork Jul 29 '13 at 15:56
    
… which is handled by the final example. –  MikeyB Jul 29 '13 at 16:43
    
Oops, indeed. Sorry, I missed that. –  manatwork Jul 29 '13 at 16:55
add comment

Using Bash:

while read line ; do
  case $line in
    PASSWORD=*)
      echo "${line#PASSWORD=}" ;;
  esac
done < "$PASSWORD.ini"

The ${variable#text} part strips the text from the variable.

share|improve this answer
add comment

And here's a Perl one:

pw=$(perl -ne 'print $1 if /PASSWORD=(.+)/' "$PASSWORD.ini")
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.