Showing results for 
Search instead for 
Do you mean 

sed / awk replace

Go to Solution
Valued Contributor

sed / awk replace


I have a string in a template, I want to build a date query into..

The original string is below:

I have a template with that entry in. I want to use this template to do a search/replace string based on standard unix date for "today"
SMON and EMON = `date +%m`
SDAY and EDAY = `date +d`
SYEAR and EYEAR = `date +Y`

can someone show me the reg expression on which characters I'd need to escape? I am guessing the & and = need escaped, Any other ideas on what I could do?

I appreciate it.. Rex M
Honored Contributor

Re: sed / awk replace


What the template do you want to replace?? What portion of the string noted should be replaced, as you're getting strings of numbers, so I guess they need to go on the right side of the equals.

something like COMPLETE_DAT&SMON=$SMON.... perhaps?
Valued Contributor

Re: sed / awk replace

Old School.

The "main file" is a php curl I am needing to cron a query at night against a website/database..

If I use a copy of the original the "date" is in one location in the file:

I need to replace "04", "30", "2009" with the actual unix system date in a sed replace of the COMPLETE DATE

I need to REPLACE the dates in the COMPLETE_DATE and replace it with

day=`date +%d`
month=`date +%m`
year=`date +%Y`

I can call the PHP script as a subshell, but I need to edit the script prior to running it so the date is current.

Acclaimed Contributor

Re: sed / awk replace

>The original string is below:

(I would think if you had a template you would have MM, DD and YYYY instead of actual numbers?)

>I am guessing the & and = need escaped,

Just "&", "=" isn't special.

MM=$(date +%m)
DD=$(date +%d)
YYYY=$(date +%Y)
sed -e "s/\&SMON=04/\&SMON=$MM/" -e "s/\&EMON=04/\&EMON=$MM/" \
-e "s/\&SDAY=30/\&SDAY=$DD/" -e "s/\&EDAY=30/\&EDAY=$DD/" \
-e "s/\&SYEAR=2009/\&SYEAR=$YYYY/" -e "s/\&EYEAR=2009/\&EYEAR=$YYYY/" file
Honored Contributor

Re: sed / awk replace

> (I would think if you had a template [...]

So would I, but what do I know?

> [...] & and = need escaped [...]

For my information only, are you from
Missouri, or is there someplace else where
someone would not say "need to be escaped"?
Honored Contributor

Re: sed / awk replace

to print this out you could just use the date command with custom formatting like this:

# date +"COMPLETE_DATE&SMON="%m"&SDAY="%d"&SYEAR="%Y"&EMON="%m"&EDAY="%d"&EYEAR="%Y

Unix operates with beer.
Honored Contributor

Re: sed / awk replace

ouch, the command would be:

# date +"COMPLETE_DATE&SMON="%m"&SDAY="%d"&SYEAR="%Y"&EMON="%m"&EDAY="%d"&EYEAR="%Y

Unix operates with beer.
Valued Contributor

Re: sed / awk replace

Thanks all,
I got it working..

I just used a perl -pi -e replace.

I redefined the string in the template to readable BASEMONTH, BASEDAY, BASEYEAR then defined the variables to replace the information in the RUN file from the template.

export day=`date +%d`
export month=`date +%m`
export year=`date +%Y`

cp class_curlConnect.php.template class_curlConnect.php
perl -pi -e s/BASEMONTH/${month}/g class_curlConnect.php
perl -pi -e s/BASEDAY/${day}/g class_curlConnect.php
perl -pi -e s/BASEYEAR/${year}/g class_curlConnect.php