Simpler Navigation coming for Servers and Operating Systems
Coming soon: a much simpler Servers and Operating Systems section of the Community. We will combine many of the older boards, and you won't have to click through so many levels to get at the information you need. If you are looking for an older board and do not find it, check the consolidated boards, as the posts are still there.
Showing results for 
Search instead for 
Did 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.

Dennis Handly
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
Steven Schweda
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"?
Viktor Balogh
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.
Viktor Balogh
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