Calling sed within a Expect script

This is a discussion on Calling sed within a Expect script within the TCL forums in Programming Languages category; Is there any way to call a sed command from within a Expect script? I just cannot seem to get the syntax right. This command seems to get me close to where I need to be but I can only run it after exiting from my Expect script. cat -v OFCENG_20080821.txt | sed -e 's/M-bM-^@M-^\\/"/g' -e 's/M-bM- ^@M-^]/"/g' > OFCENG_20080821B.txt This replaces invalid characters with an ^M. I do not really want the ^M but I am sure I can figure this part out later. TIA...

Go Back   Application Development Forum > Programming Languages > TCL

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-25-2008, 12:00 PM
jbjunk1@gmail.com
Guest
 
Default Calling sed within a Expect script

Is there any way to call a sed command from within a Expect script? I
just cannot seem to get the syntax right.

This command seems to get me close to where I need to be but I can
only run it after exiting from my Expect script.


cat -v OFCENG_20080821.txt | sed -e 's/M-bM-^@M-^\\/"/g' -e 's/M-bM-
^@M-^]/"/g' > OFCENG_20080821B.txt

This replaces invalid characters with an ^M. I do not really want the
^M but I am sure I can figure this part out later.

TIA
Reply With Quote
  #2  
Old 08-25-2008, 01:08 PM
Cameron Laird
Guest
 
Default Re: Calling sed within a Expect script

In article <ae0a279e-4fad-4cb6-8a17-96aa25486fe6@m73g2000hsh.googlegroups.com>,
<jbjunk1@gmail.com> wrote:
>Is there any way to call a sed command from within a Expect script? I
>just cannot seem to get the syntax right.
>
>This command seems to get me close to where I need to be but I can
>only run it after exiting from my Expect script.
>
>
>cat -v OFCENG_20080821.txt | sed -e 's/M-bM-^@M-^\\/"/g' -e 's/M-bM-
>^@M-^]/"/g' > OFCENG_20080821B.txt
>
>This replaces invalid characters with an ^M. I do not really want the
>^M but I am sure I can figure this part out later.
>
>TIA


Expect absolutely can run sed.

While Expect can do anything sed an do, withOUT invocation of
sed, I can understand the occasional convenience of passing
work off to a co-operating process.

Be aware that not all versions of cat(1) respect -v.

If I understand you correctly, perhaps

exec cat -v $IN | sed -e {s/M-bM-^@M-^\\/"/g} -e {s/M-bM-^@M-^]/"/g} > $OUT

will interest you. <URL:
http://phaseit.net/claird/comp.lang.tcl/fmm.html#awk > has a little
more to say on this same subject.
Reply With Quote
  #3  
Old 08-25-2008, 01:08 PM
Glenn Jackman
Guest
 
Default Re: Calling sed within a Expect script

At 2008-08-25 12:00PM, "jbjunk1@gmail.com" wrote:
> Is there any way to call a sed command from within a Expect script? I
> just cannot seem to get the syntax right.
>
> This command seems to get me close to where I need to be but I can
> only run it after exiting from my Expect script.
>
>
> cat -v OFCENG_20080821.txt | sed -e 's/M-bM-^@M-^\\/"/g' -e 's/M-bM-
> ^@M-^]/"/g' > OFCENG_20080821B.txt
>
> This replaces invalid characters with an ^M. I do not really want the
> ^M but I am sure I can figure this part out later.


Surely you can do this inside your expect script.

call [log_file] with no arguments to turn off logging, then

set logfile OFCENG_${date}.txt

set in [open $logfile r]
set out [open $logfile.new w]

while {[gets $in line] > -1} {
# replace all "funny" chars with a dot
regsub -all {[^[:graph:][:blank:]]} $line "." line
puts $out $line
}
close $in
close $out

file delete $logfile
file rename $logfile.new $logfile

--
Glenn Jackman
Write a wise saying and your name will live forever. -- Anonymous
Reply With Quote
  #4  
Old 08-25-2008, 01:53 PM
jbjunk1@gmail.com
Guest
 
Default Re: Calling sed within a Expect script

On Aug 25, 12:08*pm, Glenn Jackman <gle...@ncf.ca> wrote:
> At 2008-08-25 12:00PM, "jbju...@gmail.com" wrote:
>
> > *Is there any way to call asedcommand from within a Expect script? I
> > *just cannot seem to get the syntax right.

>
> > *This command seems to get me close to where I need to be but I can
> > *only run it after exiting from my Expect script.

>
> > *cat -v OFCENG_20080821.txt |sed-e 's/M-bM-^@M-^\\/"/g' -e 's/M-bM-
> > *^@M-^]/"/g' *> OFCENG_20080821B.txt

>
> > *This replaces invalid characters with an ^M. I do not really want the
> > *^M but I am sure I can figure this part out later.

>
> Surely you can do this inside your expect script.
>
> call [log_file] with no arguments to turn off logging, then
>
> * * set logfile OFCENG_${date}.txt
>
> * * set in [open $logfile r]
> * * set out [open $logfile.new w]
>
> * * while {[gets $in line] > -1} {
> * * * * # replace all "funny" chars with a dot
> * * * * regsub -all {[^[:graph:][:blank:]]} $line "." line
> * * * * puts $out $line
> * * }
> * * close $in
> * * close $out
>
> * * file delete $logfile
> * * file rename $logfile.new $logfile
>
> --
> Glenn Jackman
> * * Write a wise saying and your name will live forever. -- Anonymous




Earlier in my script I am sending everything to this log

set OFCENG OFCENG2_${tdate}.txt

exp_log_file /var/www/NoOffice/Input/$OFCENG


I brought in your section at the end of the script:

set logfile $OFCENG

set in [open $logfile r]
set out [open $logfile.new w]

while {[gets $in line] > -1} {
# replace all "funny" chars with a dot
regsub -all {[^[:graph:][:blank:]]} $line "." line
puts $out $line
}
close $in
close $out

file delete $logfile
file rename $logfile.new $logfile

I do not get any erros with this but my output file still has all of
special Characters:

Last login: Mon Aug 25 13:42:26 from 10.32.19.172



Shouldn't these be replaced with the *.*
Reply With Quote
  #5  
Old 08-25-2008, 02:19 PM
Cameron Laird
Guest
 
Default Re: Calling sed within a Expect script

In article <slrngb5pog.ife.glennj@smeagol.ncf.ca>,
Glenn Jackman <glennj@ncf.ca> wrote:
Reply With Quote
  #6  
Old 08-25-2008, 02:49 PM
Glenn Jackman
Guest
 
Default Re: Calling sed within a Expect script

At 2008-08-25 01:53PM, "jbjunk1@gmail.com" wrote:
> I brought in your section at the end of the script:
>


make sure you:

log_file ;# to turn off logging


> set logfile $OFCENG
>
> set in [open $logfile r]
> set out [open $logfile.new w]
>
> while {[gets $in line] > -1} {
> # replace all "funny" chars with a dot
> regsub -all {[^[:graph:][:blank:]]} $line "." line
> puts $out $line
> }
> close $in
> close $out
>
> file delete $logfile
> file rename $logfile.new $logfile
>
> I do not get any erros with this but my output file still has all of
> special Characters:
>
> Last login: Mon Aug 25 13:42:26 from 10.32.19.172
>
> 
>
> Shouldn't these be replaced with the *.*



--
Glenn Jackman
Write a wise saying and your name will live forever. -- Anonymous
Reply With Quote
  #7  
Old 08-25-2008, 03:16 PM
Glenn Jackman
Guest
 
Default Re: Calling sed within a Expect script

At 2008-08-25 02:19PM, "Cameron Laird" wrote:
> In article <slrngb5pog.ife.glennj@smeagol.ncf.ca>,
> Glenn Jackman <glennj@ncf.ca> wrote:
> .
> .
> .
> > set logfile OFCENG_${date}.txt
> >
> > set in [open $logfile r]
> > set out [open $logfile.new w]
> >
> > while {[gets $in line] > -1} {
> > # replace all "funny" chars with a dot
> > regsub -all {[^[:graph:][:blank:]]} $line "." line
> > puts $out $line
> > }
> > close $in
> > close $out
> >
> > file delete $logfile
> > file rename $logfile.new $logfile

> .
> .
> .
> Glenn, I assume you know that in many situations it's fine to
> reduce line count with something like
>
> puts $out -nonewline [regsub -all $pattern [read $in] .]


Thanks for the reminder. Largely thanks to Perl, I tend to think in a
line oriented way.

--
Glenn Jackman
Write a wise saying and your name will live forever. -- Anonymous
Reply With Quote
  #8  
Old 08-25-2008, 03:28 PM
jbjunk1@gmail.com
Guest
 
Default Re: Calling sed within a Expect script

On Aug 25, 1:49*pm, Glenn Jackman <gle...@ncf.ca> wrote:
> At 2008-08-25 01:53PM, "jbju...@gmail.com" wrote:
>
> > *I brought in your section at the end of the script:

>
> make sure you:
>
> * * log_file *;# to turn off logging
>
>
>
> > *set logfile $OFCENG

>
> > * * *set in [open $logfile r]
> > * * *set out [open $logfile.new w]

>
> > * * *while {[gets $in line] > -1} {
> > * * * * *# replace all "funny" chars with a dot
> > * * * * *regsub -all {[^[:graph:][:blank:]]} $line "." line
> > * * * * *puts $out $line
> > * * *}
> > * * *close $in
> > * * *close $out

>
> > * * *file delete $logfile
> > * * *file rename $logfile.new $logfile

>
> > *I do not get any erros with this but my output file still has all of
> > *special Characters:

>
> > *Last login: Mon Aug 25 13:42:26 from 10.32.19.172

>
> > * [H [J

>
> > *Shouldn't these be replaced with the *.*

>
> --
> Glenn Jackman
> * * Write a wise saying and your name will live forever. -- Anonymous


Glen,

I added the log_file above set logfile OFCENG_${date}.txt

I am still getting the weird characters in the output log



Example:



Press <enter> to continue...



Unfortunately, the Special Characters do not make it into the message
board post.
Reply With Quote
  #9  
Old 08-25-2008, 05:32 PM
Cameron Laird
Guest
 
Default Re: Calling sed within a Expect script

In article <cf79e8a6-8aa1-4043-a171-692b9722ab2a@z72g2000hsb.googlegroups.com>,
<jbjunk1@gmail.com> wrote:
Reply With Quote
  #10  
Old 08-26-2008, 12:02 PM
JB
Guest
 
Default Re: Calling sed within a Expect script

On Aug 26, 9:43*am, JB <jbju...@gmail.com> wrote:
> On Aug 26, 8:58*am, JB <jbju...@gmail.com> wrote:
>
>
>
> > On Aug 26, 8:42*am, JB <jbju...@gmail.com> wrote:

>
> > > On Aug 25, 4:32*pm, cla...@lairds.us (Cameron Laird) wrote:

>
> > > > In article <cf79e8a6-8aa1-4043-a171-692b9722a...@z72g2000hsb.googlegroups.com>,*<jbju. ..@gmail.com> wrote:

>
> > > > * * * * * * * * * * * * .
> > > > * * * * * * * * * * * * .
> > > > * * * * * * * * * * * * .

>
> > > > >I added the log_file above set logfile OFCENG_${date}.txt

>
> > > > >I am still getting the weird characters in the output log

>
> > > > >Example:

>
> > > > >Press <enter> to continue...

>
> > > > > [H [J

>
> > > > >Unfortunately, the Special Characters do not make it into the message
> > > > >board post.

>
> > > > Something's fishy; Glenn's code *can't* print the special characters
> > > > as you describe. *Is it possible, for example, that you're somehow
> > > > dealing with '^' followed by 'J', rather than the non-printable
> > > > rendered as '^J'?

>
> > > How would I modify the regsub to remove '^' as well if this is the
> > > case?

>
> > I figured out how to add the "^" but this was not the problem. The
> > problem is that the file is filled with ESC characters.

>
> Actually CRLF



Finally, I got it. Big thanks to Glen and Cameron, you guys are
awesome.

Here is the final version:

log_file

set logfile /var/www/NoOffice/Input/$OFCENG

set in [open $logfile r]
set out [open $logfile.new w]

while {[gets $in line] > -1} {
# replace all "funny" chars with a dot
regsub -all {[^[:graph:][:blank:][\x00-\x08]|[\x0A-\x0C]|[\x0E-
\x1F]} $line "" line

# not required, just house cleaning
regsub -all {[\[H\[J]} $line "" line

puts $out $line
}
close $in
close $out

file delete $logfile
file rename $logfile.new $logfile


Hopefully this will help others that are having the same issue.
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 09:10 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.