1825604 Members
2782 Online
109682 Solutions
New Discussion

script not working

 
SOLVED
Go to solution
lawrenzo
Trusted Contributor

script not working

Hello,

I am trying to use the sed command to change a file however am having difficulty because of some characters in the string I am trying to use - 'date +%d%m'1

for i in spfctl.001.cfg.adhoc spfdat.001.cfg.adhoc
do
cat $i |sed s/'date +%d%m'1/$SPFCHANGE/g >tmpfile
cp tmpfile $i
done
rm tmpfile

if [[ $? != 0 ]]
then
echo "Failed to change the date, please check your syntax"

can someone provide the correct syntax as I have tried numerous ways

thnx
hello
12 REPLIES 12
Mark Grant
Honored Contributor
Solution

Re: script not working

Something like

cat $i | sed "s/`date +%d%m`1/$SPFCHANGE/g" > tmpfile
Never preceed any demonstration with anything more predictive than "watch this"
Sridhar Bhaskarla
Honored Contributor

Re: script not working

Hi Lawrenzo,

Look at your previous question on the same script where I mentioned that your 'sed' was not going to work and noted on how to fix it.

http://forums1.itrc.hp.com/service/forums/que
stionanswer.do?threadId=471387

//
'date +%d%m' inside sed will be treated as a string.

cat $i |sed s/'date +%d%m'/$SPFCHANGE/g >tmpfile

Modify the above as follows

DATE=$(date +%d%m)
sed 's/'${DATE}'/'$SPFCHANGE'/g' $i > tmpfile
//


-Sri
You may be disappointed if you fail, but you are doomed if you don't try
lawrenzo
Trusted Contributor

Re: script not working

Hello Sri,

I have attempted this however the file that I am trying to change the string in isn't happening.

any other suggestions?

Tnx
hello
Mark Grant
Honored Contributor

Re: script not working

Sridhar,

Actually, on my system `date`is not treated as a string by "sed". It does what you would hope :)

Lawrenzo,

If you remove the single quotes around your variabel and use Sridhars method it should work.

i.e.

$DATE=$(date +%d%m)1 # assuming that "1" isn't a typo
cat $i| sed "s/$DATE/$SPFCHANGE/g" > tmpfile

Never preceed any demonstration with anything more predictive than "watch this"
john korterman
Honored Contributor

Re: script not working

Hi,
could you show us a few lines of input?

regards,
John K.
it would be nice if you always got a second chance
Sridhar Bhaskarla
Honored Contributor

Re: script not working

Mark,

I agree. But he was using quotes instead of backticks unlike you. So, I gave him DATE=$(...) answer in the other post.

Lawrenzo,

Can you post a sample of data?. I believe your input files do not contain the pattern given by date +%d%m command.

-Sri

You may be disappointed if you fail, but you are doomed if you don't try
lawrenzo
Trusted Contributor

Re: script not working

yes here is a couple of the lines, these appear about 10 times within the source file:

TRANSFER_TYPE=send
LOCAL_FILEPATH=/sas/analysis/four/sigpack/util/data01/prod/sas_output/SIG`date +%d%m`1.dat

REMOTE_FILEPATH=/data/cdirect/transit/prod_spf/SIG`date +%d%m`01.dat

I know the second string here is `01 therefor the original for i in * will be run twice within the script unless there is another way?

Thanks again
hello
john korterman
Honored Contributor

Re: script not working

Hi,
maybe I do not understand, but have you tried this:
sed s/'`date +%d%m`01'/$SPFCHANGE/g
or just
sed s/'`date +%d%m`'/$SPFCHANGE/g

regards,
John K.


it would be nice if you always got a second chance
Mark Grant
Honored Contributor

Re: script not working

lawrenzo,

There are about three answers here that will probably work, have you actually tried them? we haven't had any feedback from you.

I would suggest cut & paste for the sed commands because the type of quote marks you use is important and not too easy to see in a web browser.
Never preceed any demonstration with anything more predictive than "watch this"
Sridhar Bhaskarla
Honored Contributor

Re: script not working

OK. Looking at your input data, I believe you are trying to *literally* change this string. Then try this.

sed 's/\`date +%d%m\`1/'$SPFCHANGE'/g' data > tmpfile

Escape (\) the backticks (`) and see if it works.

As Mark said, copy and paste the code otherwise, you will get confused between back ticks and quotes.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
lawrenzo
Trusted Contributor

Re: script not working

Hello All,

Sorry for the late response, I was away from my desk most of today,

Thanks for the solutions, Mark and Sri's syntax both work

many thanks

Lawrenzo
hello
Rory R Hammond
Trusted Contributor

Re: script not working

I was able to make it work by using the following:

for i in spfctl.001.cfg.adhoc spfdat.001.cfg.adhoc
do
cat $i |sed s/$(date +%d%m)1/$SPFCHANGE/g >tmpfile
cp tmpfile $i
done
There are a 100 ways to do things and 97 of them are right