1833315 Members
2981 Online
110051 Solutions
New Discussion

More Script Help

 
SOLVED
Go to solution
Greg Stark_1
Frequent Advisor

More Script Help

I'm trying to write a script that will purge a faxing database and I'm hung up on the way the shell interprets the following line:

faxstats -OUTBOUND -admin -all |sed '/$UPTO/,\$d'

where $UPTO is a date variable, i.e. "Apr 15"

If I surround the command in echo "" it prints the correct command and information. But, when I run it from a script I get the following error:

sed: 0602-404 Function /$UPTO/,\$d cannot be parsed.

Any ideas?

Thanks again,
Greg
8 REPLIES 8
A. Clay Stephenson
Acclaimed Contributor

Re: More Script Help

Hi Greg:

I'm not sure that I quite understand what you want to do but if all you want to do is delete all lines that have 'Apr 15' in them then this should do it:

UPTO="Apr 15"
faxstats -OUTBOUND -admin -all |sed "/${UPTO}/d"

Note the use of the double quotes to allow the varuable to be evaluated.


If it ain't broke, I can fix that.
Sachin Patel
Honored Contributor

Re: More Script Help

Hi Greg,
I thing you are trying to replace $UPTO by ",$d"

example
# t="somethis apr 15"
# t1="apr 15"

replace apr 15 by ttt

# echo $t |sed "s/$t1/ttt/g"

Sachin


Is photography a hobby or another way to spend $
Greg Stark_1
Frequent Advisor

Re: More Script Help

Perhaps I should give some more info. The fax software can delete entries in the database up to a certain date. I want to run this script each day and delete all entries older than 14 days (this is what you helped me with on Friday - caljd.sh).

Before actually deleting the fax entries, I want to log which entries will be deleted. To do this I am using the "faxstats -OUTBOUND -admin -all" command to do a listing of the current entries in the database and sed to parse out the entries that are before the given $UPTO date. The faxstats/sed command works from the command line. It also seems to be loading the correct date variable when I echo the command in the script.

I have attached the script so far as well as a portion of the output of faxstats -OUTBOUND -admin -all.

Thanks again,
Greg
A. Clay Stephenson
Acclaimed Contributor

Re: More Script Help

Okay Greg, you could use grep to do this but you are wise to use sed to both print the pattern AND to do the deletion - that way you know that both operations zig or zag in unison.

To print the lines to be deleted:

UPTO="Apr 15"
faxstats -OUTBOUND -admin -all |sed -n "/${UPTO}/p"


Note the use of the -n option to suppress the default output.

If it ain't broke, I can fix that.
Sachin Patel
Honored Contributor

Re: More Script Help

Hi Greg,
If I understand correctly then you want to delete fax data older then 14 days and before you delete you want to save that to log file.

faxstats -OUTBOUND -admin -all | grep ${UPTO} > fax.logfile

then
as Clay says delete it
faxstats -OUTBOUND -admin -all |sed "/${UPTO}/d"


Sachin
Is photography a hobby or another way to spend $
Mark Fenton
Esteemed Contributor
Solution

Re: More Script Help

Greg,

shouldn't that work if you fed sed double quotes instead of single? Single quotes force passing literal contents, whereas double quotes allow shell to expand expressions, which is what you want, no?

Clay -- school me if I've got this wrong...
Peter Kloetgen
Esteemed Contributor

Re: More Script Help

Hi Greg,

this is a quoting problem:

\ quotes one meta character, which follows directly

'quoted_string' quotes EVERY meta character inside, the only thing not quoted is " ' " itself! No variable subsittution or command substitution inside of quoting! ( This is your problem.... )

"quoted_string" -->>> some meta characters are quoted, but within quoting variable substitution and command substitution is done!

So this should do it for you! Simply use "" instead of '' for quoting.

Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
Greg Stark_1
Frequent Advisor

Re: More Script Help

Thanks Mark and Peter! That did it. Clay, I'm sure if I would have explained it better, you would have known what I was getting at.

Thanks again,
Greg