I have file with two space separated word in each line. I am looking for shell scrip in Unix to replace the first word of each row with its md5 hash representation.

I have tried to adjust what is mentioned in Awk replace a column with its hash value.

But, it what this solution returns for md5 hash value looks wrong.

md5(is) = a2a551a6458a8de22446cc76d639a9e9
  • 1
    That's because the awk answers there use echo and that adds a newline... Replace it with printf %s you'll get the right hash: a2a551a6458a8de22446cc76d639a9e9 – don_crissti Sep 2 '15 at 20:34

For an inefficient and possibly suboptimal answer:

while read -r first rest; do
    echo "$(echo -n $first | md5sum | awk '{ print $1 }') $rest"
done <input_file

This will mangle the whitespace between the first word and the rest of the line, but should preserve that between subsequent words, if any.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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