1

In my Unix AIX I need to replace the date in a parameter file with system date using a shell script that will run post session from PowerCenter.

I can use SED to replace a string variable but running into issue using date.

I have tried using commas instead of slashes as delimiters. The contents of the test file looks like this:

$$TESTFIELD=12/29/2016 14:57:51

The sed command I felt should work was:

sed -e "s,^\($$TESTFIELD=\).*,\1'date +"%D %T"'," testfile.txt > tmp.$$

After which I would move the tmp.$$ to testfile.txt but the results threw the following error:

sed: 0602-404 Function s,^\(14680298TESTFIELD=\).*,\1'date +%D cannot be parsed.
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • note that you are using double quotes, so `sed -e "s..."` expands `$$TESTFIELD` to its value. – fedorqui Jan 18 '17 at 15:15
  • You may want to have a look to [How to change date format in sed?](http://stackoverflow.com/a/34080390/1983854) also, to see how to execute an external command from within `sed`. Basically, you need to use the `-e` flag. – fedorqui Jan 18 '17 at 15:18
  • Maybe: `Now=$(date +"%D %T"); sed "s,=.*$,=$Now," testfile.txt >>tmp.$$` – Lorinczy Zsigmond Jan 18 '17 at 17:34

2 Answers2

2

You need to use single quotes or escape the $ in the sed command. Otherwise the shell would expand $TESTFIELD. I recommend the first in this case:

sed -e 's,^\($$TESTFIELD=\).*,\1'\''date +"%D %T"'\'','

Your question is quite unclear. If you want to replace the datetime in the file by the current datetime, use:

sed -e 's,^\($$TESTFIELD=\).*,\1'"$(date +"%D %T")"','
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
0
sed -e 's,^\($$TESTFIELD=\).*,\1'"$(date +"%D %T")"','

This worked perfectly. Results: $$TESTFIELD=01/18/17 13:14:59