1

I have written a script finding the hash value from a dictionary and outputting it in the form "word:md5sum" for each word. I then have a file of names which I would like to use to place each name followed by every hash value i.e. tom:word1hash tom:word2hash . . bob:word1hash

and so on. Everything works fine but I can not figure out the substitution. Here is my script.

$#!/bin/bash
#/etc/dictionaries-common/words
cat words.txt | while read line; do echo -n "$line:" >> dbHashFile.txt  
echo "$line" | md5sum | sed 's/[ ]-//g' >> dbHashFile.txt; done
cat users.txt | while read name
do
cat dbHashFile.txt >> nameHash.txt;
awk '{$1="$name"}' nameHash.txt;
cat nameHash.txt >> dbHash.txt;
done

the line

    $awk '{$1="$name"}' nameHash.txt;

is where I attempt to do the substitution.

thank you for your help

1 Answer 1

2

Try replacing the entire contents of the last loop (both cats and the awk) with:

awk -v name="$name" -F ':' '{ print name ":" $2 }' dbHashFile.txt >>dbHash.txt
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. It works perfect. I wish I could +1 you. So you must redeclare variables when used in an awk expression?
Awk won't take variables from the environment, no. Of course you could also do awk -F : { print "'"$name"'" ":" $2 }' instead. (As for +1, I believe you can still accept the answer by clicking the checkmark thingy, which you should do if it worked for you. =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.