0

I am new in using sed command and I have an sql which contains "MONYY" and I want to change the "MONYY" in the file by replacing it with the current "MONYY" automatically.

I have used the following command and I am able to get previous month and year and current month and year in uppercase and now I am trying to replace the values in my script using sed command but nothing is happening.

date1=$ date +"%b%y"|sed 's/\(.*\)/\U\1/'

echo $date1

date2=$ date -d ' -1 month ' +"%b%y"|sed 's/\(.*\)/\U\1/'

echo $date2

sed 's/$date1/$date2/g' aggdom.sql > temp

Can anyone help me out with the sed command that I am using

1 Answer 1

1

The script above will substitute every $date1 with $date2

but the result will be printed in temp file

If you want to change the strings in aggdom.sql

you have to change the line to something like that:

sed -i 's/$date1/$date2/g' aggdom.sql 

from sed man page:

-i[SUFFIX], --in-place[=SUFFIX]

         edit files in place (makes backup if extension  supplied).   The
          default  operation  mode  is  to  break symbolic and hard links.
          This can be changed with --follow-symlinks and --copy.

on some operating system you have to add -e as an option:

sed -i -e s/$date1/$date2/g aggdom.sql
4
  • Hello Networker, you were right the first time. I am trying to every $date1 with $date2 into another file. But I have tried the command you have provided and still no changes is being found in aggdom.sql. The date remains as it was.
    – user342220
    Jul 8, 2014 at 9:24
  • what the OS you are using
    – Nidal
    Jul 8, 2014 at 9:40
  • I am using GNU/Linux
    – user342220
    Jul 8, 2014 at 9:49
  • even when adding -e also its the same. Is there any other command that I can use instead of sed?
    – user342220
    Jul 8, 2014 at 10:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .