Use Variable in SED

Viewed 108

I cannot expand this variable in sed. I've tried everything I can think of.

I am trying to put the md5sum of file1 in line 10 of file2

I can take $x out of the regex and put some text and it works. It just will not accept the variable. printf the variable is fine.

#!/bin/bash
 x=$(md5sum /etc/file1)
 printf "$x \n"
 sed -i 10"s/.*/$x/g" /usr/bin/file2
2 Answers

You may use this command that uses ~ as regex delimiter instead of / since output of md5sum contains /:

sed -i "10s~.*~$x~" /usr/bin/file2

After I reduced the variable from the md5sum output which includes the filename and directory by running $x thru:

x=$(echo $x | head -n1 | awk '{print $1;}')

Leaving only the MD5 it worked and quit erroring.

Related