Home > Software design >  How to execute the output of another command
How to execute the output of another command

Time:10-16

I have a CSV file where I would like to modify the following string:

${GDATEF(-4D,ddMMyyyy)}

To do so I am using the following script:

#!/usr/bin/env bash

echo "replacing >>D,ddMMyyyy)<<"
sed -i "s/D,ddMMyyyy)\}/ days\\\\\"\ \ \\\\\"%d\/%m\/%Y\\\\\"/g" $1


echo "replacing >>\${GDATEF(<< AND executing date   N days"
sed -i "s/\${GDATEF(/date -d \\\\\"`date`   /g" $1

echo "the final touch ;)"
sed -i "s/date -d /date -d/g" $1

This results in:

date -d\"Fri Oct 15 10:38:20 UTC 2021   -4 days\"  \"%d/%m/%Y\"

Now I can take that result, remove some unnecessary characters and execute it manually:

sh-4.2# date -d "Fri Oct 15 09:20:53 UTC 2021   -4 days"  "%d/%m/%Y"
11/10/2021

However, I would like to be able to perform that last step automatically, any ideas?

CodePudding user response:

Take the final result and use sed to remove \:

echo "date -d\"Fri Oct 15 10:38:20 UTC 2021   -4 days\"  \"%d/%m/%Y\"" | sed 's \\  '

This will output:

date -d"Fri Oct 15 10:38:20 UTC 2021   -4 days"  "%d/%m/%Y"

If you execute that you will get:

11/10/2021

TL;DR

Here is the all in one solution:

eval $(echo "date -d\"Fri Oct 15 10:38:20 UTC 2021   -4 days\"  \"%d/%m/%Y\"" | sed 's \\  ')
  • Related