Unix Shell - sed script help.

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > February 2006 > sed script help.





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author sed script help.
charlie

2006-02-26, 10:19 am

Hi,
I've been debugging some VBScript and so to help me I wanted to add a
logging message to the beginning and end of each function or sub that
logs the name of the sub or fun. The script I came up with and example
input and output follows.
The problem is that the ") chars after the \1 in both cases seem to get
moved to the front of the pattern buffer and overwrite the wr of
writelog. If I don't follow the \1 with anything then the command works
as you (I) would expect.
What am I doing wrong? Also I'm sure my sed style is poor (this is my
first non-s sed use) so please critique away.

Cheers,
Charlie

#!/usr/bin/sed -f

# Match the start of the function
/\(Sub\|Function\|sub\|function\) .*/{
P
# Strip of the declaration leaving only the name and args
s/.* \(.*\)/\1/g
# Save the name and args to the hold buffer
h
# write the logging command
s/\(.*\)/writelog("DBG:Entering \1")/g
}

# Match the end of the function
/\(End\|end\) \(Sub\|Function\|sub\|function\)/{
# recall the name from above
x
# write to the log
s/\(.*\)/writelog("DBG: Leaving \1")/g
P
# Get the end statement back
x
}

# ------------------- END OF SCRIPT ----------------------

Input:

Set fso = CreateObject("Scripting.FileSystemObject")
Set theScript = CreateObject("MSScriptControl.ScriptControl")
inited = 0

'End of (Declarations)

Function AddCodeFile(strFile)
'Copied from ifixup, reads in a script file
'and returns a string of its contents
' On Error Resume Next
MsgBox("AddCodeFile: OpeningTextFile: " + strFile)
Set f = fso.OpenTextFile(strFile, 1)
' If TrapError("ReadScript : " & strFile) Then
' MsgBox("AddCodeFile: Failed to OpenTextFile: " +
Error(Err))
' Exit Function
' End If
sScript=f.ReadAll()
f.Close()
Set f=Nothing
AddCodeFile=sScript
' On Error Goto 0
End Function

Function Test
Dim code
MsgBox("Test: Started")
theScript.Language="VBScript"
theScript.Reset
theScript.AllowUI=True
theScript.Timeout = -1
code =
AddCodeFile(" C:\Datacap\BPilot\Scan_task_project\ISsc
an\test2.vbs
")
MsgBox("Test: Code: " + code)
Call theScript.AddCode(code)
MsgBox("Test: Added Code")
Call theScript.Run("RunMe")
Call theScript.Run("RunMeWithArgs", "Arg1", "Arg2")
End Function

Test

' ---------------- END OF SCRIPT ------------------------
Output:

Set fso = CreateObject("Scripting.FileSystemObject")
Set theScript = CreateObject("MSScriptControl.ScriptControl")
inited = 0

'End of (Declarations)

Function AddCodeFile(strFile)
")itelog("DBG: Entering AddCodeFile(strFile)
'Copied from ifixup, reads in a script file
'and returns a string of its contents
' On Error Resume Next
MsgBox("AddCodeFile: OpeningTextFile: " + strFile)
Set f = fso.OpenTextFile(strFile, 1)
' If TrapError("ReadScript : " & strFile) Then
' MsgBox("AddCodeFile: Failed to OpenTextFile: " +
Error(Err))
' Exit Function
' End If
sScript=f.ReadAll()
f.Close()
Set f=Nothing
AddCodeFile=sScript
' On Error Goto 0
")itelog("DBG: Leaving AddCodeFile(strFile)
End Function

Function Test
")itelog("DBG: Entering Test
Dim code
MsgBox("Test: Started")
theScript.Language="VBScript"
theScript.Reset
theScript.AllowUI=True
theScript.Timeout = -1
code =
AddCodeFile(" C:\Datacap\BPilot\Scan_task_project\ISsc
an\test2.vbs
")
MsgBox("Test: Code: " + code)
Call theScript.AddCode(code)
MsgBox("Test: Added Code")
Call theScript.Run("RunMe")
Call theScript.Run("RunMeWithArgs", "Arg1", "Arg2")
")itelog("DBG: Leaving Test
End Function

Test

' ---------------- END OF SCRIPT ------------------------

Bill Marcum

2006-02-26, 10:19 am

On 24 Feb 2006 12:36:17 -0800, charlie
<charlie.burrows@gmail.com> wrote:
> Hi,
> I've been debugging some VBScript and so to help me I wanted to add a
> logging message to the beginning and end of each function or sub that
> logs the name of the sub or fun. The script I came up with and example
> input and output follows.
> The problem is that the ") chars after the \1 in both cases seem to get
> moved to the front of the pattern buffer and overwrite the wr of
> writelog. If I don't follow the \1 with anything then the command works
> as you (I) would expect.


Since you are editing VBScript files, which are only used in Windows,
these files probably contain a CR at the end of each line. Your sed
script adds ") after the CR.


--
Doing gets it done.
Bill Seivert

2006-02-27, 2:48 am



charlie wrote:
> Hi,
> I've been debugging some VBScript and so to help me I wanted to add a
> logging message to the beginning and end of each function or sub that
> logs the name of the sub or fun. The script I came up with and example
> input and output follows.
> The problem is that the ") chars after the \1 in both cases seem to get
> moved to the front of the pattern buffer and overwrite the wr of
> writelog. If I don't follow the \1 with anything then the command works
> as you (I) would expect.
> What am I doing wrong? Also I'm sure my sed style is poor (this is my
> first non-s sed use) so please critique away.
>
> Cheers,
> Charlie
>
> #!/usr/bin/sed -f
>
> # Match the start of the function
> /\(Sub\|Function\|sub\|function\) .*/{
> P
> # Strip of the declaration leaving only the name and args
> s/.* \(.*\)/\1/g

Since regular expressions are greedy, anchor this expression
and make it skip only the first field, e.g.,
s/^[^ ]* \(.*\)/\1/

The first ^ says begin at the start of the string, the [^ ]*
matches zero or more non-spaces,
The g is not necessary.

Bill Seivert

charlie

2006-02-27, 8:48 pm

Thank-you both. As it turns out the solution was to anchor both the
start and end of the expression so that I could strip the windowsy CR:
s/^[^ ]* \(.*\).$/\1/
I've never seen the deleterous effect of a bare CR before. I will
beware in future!

BTW how can I specifically match the CR character. I tried \r, \\r ^M
etc but none seemed to match; also the expression [a-zA-z,()0-9] seems
to match the CR character which I found quite odd.
Anyway it is done so thank-you.
Charlie

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com