Using commands inside sed substitution

Dear Friends,

Please let me know how to use the date command inside the substitution flag replacement string.

echo "01 Jan 2003:11:00:06 +0100" | sed 's/\(.*\)/`date -d \1 "+%Y%m%d%H%M%S"`/'

I want to supply \1 string to Here mention below as part of replacement string,

date -d <Here> "+%Y%m%d%H%M%S"

However my sed version is not working. please let me know the right way to do this.

Thanks in advance,

I understand you are trying to replace the entire statement with the date command. In that case,

$ DATE=`date -d \1 "+%Y%m%d%H%M%S"`

$ echo "01 Jan 2003:11:00:06 +0100" | sed "s/.*/$DATE/"
20110921010000

Guru.

Guruprasad,

Thanks for your response. However I am not looking this one. I exactly want to know how to use date command in substitution replace string. a log file will be read from the pipeline where each line will have a date string which has to be replaced with the date in different format(Numerical).

I don't think sed will work in this situation. Something like this is more along the lines of what you'll need. It assumes the date you want to transform is the first 5 tokens on each record. It can easily be adapted to convert the date from anyplace in the string, but as most log files contain the date up front, I assumed it was first.

 awk '
    function execute( cmd,  r )   # execute command, return last line of output from command. 
    {
        while( (cmd | getline) )
            r = $0;

        close( cmd );    # very important to close the pipe
        return r;
    }

    {
        $5 = $5 "~";    # assume fields 1-5 are the date
        split( $0, a, "~" );
        new_date = execute( sprintf( "date -d \"%s\" \"+%%Y%%m%%d%%H%%M%%S\"", a[1] ) );
        print new_date  a[2];
    }
' log-file-name
$ echo "NNabcNN"  | sed -e 's/abc/'"`date`"'/g'
NNTue Sep 20 22:22:53 EDT 2011NN

fpmurphy,

Thanks for your response. Your suggestion partially satisfies my requirement. However Here abc will be replaced by the current date always. I want to pass "abc" as the input to the data command in the replacement string so that the date will be displayed with respect to "abc" (abc has to be replace by the log date)