Formatting File Decryption Output

Below is the out put of the decrypt command.

Decrypt Command:
/usr/bin/gpg --yes --batch --output file.xml --decrypt file.xml.gpg

Output:

gpg: encrypted with 2048-bit RSA key, ID 96301328, created 2014-04-29
      "XYZ <xyz@abc.com>"
gpg: encrypted with 2048-bit ELG-E key, ID ECB614CF, created 2002-02-06
      "PQR <pqr@abc.com>"
gpg: encrypted with 2048-bit ELG-E key, ID 1EB07C50, created 2014-02-20
      "MNP (mnp Data Warehouse file encryption key) <mnp@abc.com>"
gpg: WARNING: message was not integrity protected

My requirement is I need to verify if total 3 keys are present in the encrypted file, the only way i think i know is when i decrypt it and then count the email id's. So how could i get the below email ids from the output i pasted above either by redirecting the output or at the time of decryption itself. After i get this i wanted to go in loop or something to increment a variable to get the count of email id's.

xyz@abc.com
pqr@abc.com
mnp@abc.com

so far i came up with code but still not getting it correctly

cat decrypt_output | grep @ decrypt_output | awk -F"<*" '{print $2}'
xyz@abc.com>"
pqr@abc.com>"
mnp@abc.com>"

appreciate your help.

awk -F"[<>]" '/@/ { print $2 }' inputfile
1 Like

I extended your code further to print the count or line number. But is there a way to print only 3 instead of 7 as it has some additional blank lines in the output file.

awk -F"[<>]" '/@/ {print $2} END{print NR}'

xyz@abc.com
pqr@abc.com
mnp@abc.com
7

To print a count, one must take a count.

awk -F"[<>]" '/@/ {L++ ; print $2} END{print L+0}'
1 Like

Hello Ariean,

One more approach for same.

awk '!/gpg/ {count++;match($0,/<.*>/);print substr($0,RSTART+1,RLENGTH-2)} END{print count}'  Input_file

Output will be as follows.

xyz@abc.com
pqr@abc.com
mnp@abc.com
3

Thanks,
R. Singh

1 Like
perl -wlne 'if(m/([\w.%+-]+@[\p{Alnum}.-]+\.\p{Alpha}{2,6})/){print $1; $i++} print $i if eof' file