0

I am attempting to write a script to update a file with the md5 of a jar file.

To get the md5, I can do the command:

 md5sum target/file1.jar | awk '{print $1;'}

This will print the md5 of the file. To use sed to replace the text ${md5}, I can do the command:

sed -i 's/${md5}/md5Output/g' File2.json 

I would like to replace md5Output with the contents of the first command.

Is this possible? Basically the goal is to calculate the md5 of "File1" and place that md5 value in "File2"

GSUgambit
  • 4,459
  • 6
  • 25
  • 31
  • Have you tried storing the result of your first command in a variable? – Stephen Newell May 17 '18 at 18:45
  • @StephenNewell I tried doing ">> variable" in the first command and using variable in the sed but it printing as a literal string not value of the variable :( – GSUgambit May 17 '18 at 18:54
  • See https://stackoverflow.com/questions/4651437/how-to-set-a-variable-to-the-output-of-a-command-in-bash and https://stackoverflow.com/questions/19151954/how-to-use-variables-in-a-command-in-sed – Benjamin W. May 17 '18 at 18:57
  • Note: the closing single quote in your awk command should be outside the curly braces. – Benjamin W. May 17 '18 at 18:57
  • 1
    See: [Difference between single and double quotes in bash](http://stackoverflow.com/q/6697753/3776858) – Cyrus May 17 '18 at 19:55

2 Answers2

1

Sed One-liner :

$ sed -i  "s/\${md5}/$( md5sum target/file1.jar | awk '{print $1}' )/g" File2.json
Gautam
  • 1,862
  • 9
  • 16
0

@Cyrus thanks for the references! Here is the complete answer for anyone who needs to do the same type of thing

 #!/bin/sh

 VARIABLE=$(md5sum target/file1.jar | awk '{print $1;}')
 sed -i "s|\${md5}|$VARIABLE|g" file2.json
GSUgambit
  • 4,459
  • 6
  • 25
  • 31