Awk replace a column with its hash value

So, you don’t really want to be doing this with awk. Any of the popular high-level scripting languages — Perl, Python, Ruby, etc. — would do this in a way that was simpler and more robust. Having said that, something like this will work.

Given input like this:

this is a test

(E.g., a row with four columns), we can replace a given column with its md5 checksum like this:

awk '{
    tmp="echo " $2 " | openssl md5 | cut -f2 -d\" \""
tmp | getline cksum
$2=cksum
print
}' < sample 

This relies on GNU awk (you’ll probably have this by default on a Linux system), and it uses openssl to generate the md5 checksum. We first build a shell command line in tmp to pass the selected column to the md5 command. Then we pipe the output into the cksum variable, and replace column 2 with the checksum. Given the sample input above, the output of this awk script would be:

this 7e1b6dbfa824d5d114e96981cededd00 a test

Leave a Comment