1745793 Members
3513 Online
108722 Solutions
New Discussion юеВ

Re: sed / awk replace

 
SOLVED
Go to solution
rmueller58
Valued Contributor

sed / awk replace

All,

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

The original string is below:
COMPLETE_DATE&SMON=04&SDAY=30&SYEAR=2009&EMON=04&EDAY=30&EYEAR=2009

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"
ie:
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
7 REPLIES 7
OldSchool
Honored Contributor

Re: sed / awk replace

huh???

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?
rmueller58
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:
COMPLETE_DATE&SMON=04&SDAY=30&SYEAR=2009&EMON=04&EDAY=30&EYEAR=2009


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
Solution

Re: sed / awk replace

>The original string is below:
COMPLETE_DATE&SMON=04&SDAY=30&SYEAR=2009&EMON=04&EDAY=30&E
YEAR=2009

(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
COMPLETE_DATE&SMON=05&SDAY=05&SYEAR=2009&EMON=05&EDAY=05&EYEAR=2009

****
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.
rmueller58
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