Reputation: 711
I am trying to use sha256sum hashing command to hash email address.
$ echo -n example@gmail.com | sha256sum | awk '{print $1}'
264e53d93759bde067fd01ef2698f98d1253c730d12f021116f02eebcfa9ace6
Now I want to apply the same on an input file with email_address only,but the below shows a different hash and looks like CAT is the culprit here,let me know how to overcome the issue.
$ echo -n | cat test.txt | sha256sum | awk '{print $1}'
5c98fab97a397b50d060913638c18f7fd42345248bb973c486b6347232e8013e
ideally ,I would like to see below if the test.txt has only one record example@gmail.com (it can have n number of email address)
example@gmail.com|264e53d93759bde067fd01ef2698f98d1253c730d12f021116f02eebcfa9ace6
Upvotes: 0
Views: 687
Reputation: 120704
It's a bit unclear, but I think your test.txt
file looks like this:
example@gmail.com rkj@stackoverflow.com sean@stackoverflow.com
And you want to produce the output:
example@gmail.com|264e53d93759bde067fd01ef2698f98d1253c730d12f021116f02eebcfa9ace6 rkj@stackoverflow.com|2a583d8e55db9bac7247cac8dc4b52780010583217844e864e159c458ce0185c sean@stackoverflow.com|797f02327b00f12486f2ed5e85e61680ecb75a0f969b54a627e329506aaf595c
If that is the case, this will do it:
while read email; do SHA=$(echo -n $email | sha256sum | awk '{print $1}'); echo "$email|$SHA"; done < test.txt
In your initial command, you are using echo -n
which prints the arguments without a trailing newline. So you are getting the hash of just the e-mail address itself. Once you start working with a file, every line is going to have a newline on it, so for each line you have to strip off the newline and get the hash. That is effectively what my suggested solution is doing.
Upvotes: 1