Vor
Vor

Reputation: 35109

How to take a hash of a line in CSV and add it as a last column

I have a file:

cat test.csv
Unix,10,A
Linux,30,B
Solaris,40,C
Fedora,20,D
Ubuntu,50,E

I want to transform it to this:

Unix,10,A,f9be1a25bec3e55418e4f6a75a6bdceecb6d6d17af911d8b4ef478431edc68d2
Linux,30,B,659c957414b20e098c299a5769f0c05b225b7fef007cd0e71e0355f7bc8afe5c
Solaris,40,C,3189a15aa81b86277e8e910eeb17a2d6a4e52fbdcbf326034d7691471788b9b7
Fedora,20,D,14a0ae4fb2a3bd2209f60969d75bee5ca243921f02be8ffc0f37f2ea9354f0b2
Ubuntu,50,E,dc635842ca6f904ca658ec71b5d9205221664688eaa028917663ab9760e823c3

I'm trying to do this:

while read line; 
do
  echo -n $line | openssl dgst -sha256 -hmac "SECRET" | cut -d' ' -f2
done < test.csv

And it gives me the desired hash, but I don't know how to add hashes at the end. I was looking at tool called awk but can't figure it out.

Upvotes: 1

Views: 1421

Answers (4)

John1024
John1024

Reputation: 113834

while read -r line
do
  echo "$line","$(echo -n $line | openssl dgst -sha256 -hmac "SECRET" | cut -d' ' -f2)"
done < test.csv

This produces the output:

Unix,10,A,f9be1a25bec3e55418e4f6a75a6bdceecb6d6d17af911d8b4ef478431edc68d2
Linux,30,B,659c957414b20e098c299a5769f0c05b225b7fef007cd0e71e0355f7bc8afe5c
Solaris,40,C,3189a15aa81b86277e8e910eeb17a2d6a4e52fbdcbf326034d7691471788b9b7
Fedora,20,D,14a0ae4fb2a3bd2209f60969d75bee5ca243921f02be8ffc0f37f2ea9354f0b2
Ubuntu,50,E,dc635842ca6f904ca658ec71b5d9205221664688eaa028917663ab9760e823c3

Upvotes: 4

Jotne
Jotne

Reputation: 41456

Here is an awk. Not sure why it give different hash, but it should work:

awk '{cmd="echo -n \""$0"\" | openssl dgst -sha256 -hmac \"SECRET\"";cmd | getline var;close(cmd);sub(/.* /,"",var);print $0 "," var}' file
Unix,10,A,f9be1a25bec3e55418e4f6a75a6bdceecb6d6d17af911d8b4ef478431edc68d2
Linux,30,B,659c957414b20e098c299a5769f0c05b225b7fef007cd0e71e0355f7bc8afe5c
Solaris,40,C,3189a15aa81b86277e8e910eeb17a2d6a4e52fbdcbf326034d7691471788b9b7
Fedora,20,D,14a0ae4fb2a3bd2209f60969d75bee5ca243921f02be8ffc0f37f2ea9354f0b2
Ubuntu,50,E,dc635842ca6f904ca658ec71b5d9205221664688eaa028917663ab9760e823c3

Or written like this:

awk '{
    cmd="echo -n \""$0"\" | openssl dgst -sha256 -hmac \"SECRET\""
    cmd | getline var
    close(cmd)
    sub(/.* /,"",var)
    print $0 "," var}
    ' test.csv

Upvotes: 2

BMW
BMW

Reputation: 45243

@ glenn, why need paste?

while read line
do  
    echo -n "$line,"
    echo -n $line | openssl dgst -sha256 -hmac "SECRET" | cut -d' ' -f2
done < test.csv

Upvotes: 1

glenn jackman
glenn jackman

Reputation: 246807

paste is a handy tool for joining lines: first print the line as-is, then print the hash, then join the lines with a comma:

while read line; do
  echo "$line"
  echo -n "$line" | openssl dgst -sha256 -hmac "SECRET" | cut -d' ' -f2
done < test.csv | 
paste -d, - -
Unix,10,A,f9be1a25bec3e55418e4f6a75a6bdceecb6d6d17af911d8b4ef478431edc68d2
Linux,30,B,659c957414b20e098c299a5769f0c05b225b7fef007cd0e71e0355f7bc8afe5c
Solaris,40,C,3189a15aa81b86277e8e910eeb17a2d6a4e52fbdcbf326034d7691471788b9b7
Fedora,20,D,14a0ae4fb2a3bd2209f60969d75bee5ca243921f02be8ffc0f37f2ea9354f0b2
Ubuntu,50,E,dc635842ca6f904ca658ec71b5d9205221664688eaa028917663ab9760e823c3

Upvotes: 2

Related Questions