Re: sed / awk replace (208 Views)
Reply
Valued Contributor
rmueller58
Posts: 851
Registered: ‎02-19-2001
Message 1 of 8 (208 Views)
Accepted Solution

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
Honored Contributor
OldSchool
Posts: 3,372
Registered: ‎09-09-2004
Message 2 of 8 (208 Views)

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?
Valued Contributor
rmueller58
Posts: 851
Registered: ‎02-19-2001
Message 3 of 8 (208 Views)

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.

Acclaimed Contributor
Dennis Handly
Posts: 25,206
Registered: ‎03-06-2006
Message 4 of 8 (208 Views)

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
Honored Contributor
Steven Schweda
Posts: 9,091
Registered: ‎02-23-2005
Message 5 of 8 (208 Views)

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
Viktor Balogh
Posts: 1,007
Registered: ‎03-15-2009
Message 6 of 8 (208 Views)

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.
Honored Contributor
Viktor Balogh
Posts: 1,007
Registered: ‎03-15-2009
Message 7 of 8 (208 Views)

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
rmueller58
Posts: 851
Registered: ‎02-19-2001
Message 8 of 8 (208 Views)

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



The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.