Page 1 of 1 1
Topic Options
#122579 - 2004-07-11 02:11 PM Strip leading spaces?
brewdude6 Offline
Hey THIS is FUN

Registered: 2000-10-21
Posts: 280
Loc: Nashville, TN
I'm searching for strings in a text file like this:

Code:
If InStr ($line,'Inbound communication address')  



The text file is structured like this:

Code:
           DCS Application Snapshot
Client application ID = 0AE67894.EB07.040702161625
Authorization ID = CXIDBC21
Application name = java.exe



I'd like to search for lines that contain "Authorization ID" etc and load the entire line, but I get the leading spaces on the left along with it. I'm not sure how to incorporate "TRIM" in this statement or if it's appropriate?

Thanks,
_________________________
I could have made a neat retort but didn't, for I was flurried and didn't think of it till I was downstairs.
-Mark Twain

Top
#122580 - 2004-07-11 03:20 PM Re: Strip leading spaces?
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Well you do not state what version of KiXtart you are using, but looking at the 4.22 manual yields:

Code:
LTrim( )

Action: Strips leading spaces from an input string and returns the result.

Syntax: LTRIM ("string")

Parameters: String
The string from which to strip leading spaces.

Returns: The input string without leading spaces.

See Also: RTrim( ), Trim( )

Example: $x = LTRIM(SUBSTR(@IPADDRESS0, 1, 3))




If you want more control to match and extract the value you should look at using Regular Expressions. Checkout this link: RFC: Regular Expression UDFs

Let me know if you have any further questions.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#122581 - 2004-07-11 03:56 PM Re: Strip leading spaces?
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
The snippet of code you show does not have anything to do with the question. Why not show us what you are trying to use?
Code:
break on
$Line = ' Authorization ID = CXIDBC21'
$AuthID = Trim(Split($Line,'=')[0])
$AuthID

_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#122582 - 2004-07-11 04:26 PM Re: Strip leading spaces?
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Since you did not post the code you have so we can integrate a solution into it, here is more complete solution.
Code:
break on
$LineNum = 0
$File = 'C:\file.txt'
$Array = ReadFile($file)
For each $Line in $Array
If Instr($Line,'Authorization ID')
'Found on line # ' $LineNum ?
$AuthID = Trim(Split($Array[$LineNum],'=')[0])
$AuthID ?
EndIf
$LineNum = $LineNum + 1
Next

;Function ReadFile()
;Author Radimus
;Contributors Lonky... This is basically his code that I trimmed down to its
; simplest function
;Action Reads a file into an array
; http://www.kixtart.org/ubbthreads/showflat.php?Cat=&Number=83675

Function ReadFile($file)
Dim $lf, $f, $_, $t
$lf=Chr(10)
$f=FreeFileHandle
$_=Open($f,$file)
If @ERROR Exit @ERROR EndIf
Do $t=$t+$lf+ReadLine($f) Until @ERROR
$_=Close($f)
$ReadFile=Split(SubStr($t,2),$lf)
EndFunction

_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#122583 - 2004-07-11 08:52 PM Re: Strip leading spaces?
brewdude6 Offline
Hey THIS is FUN

Registered: 2000-10-21
Posts: 280
Loc: Nashville, TN
Sorry for not posting more of the code. Les, I believe this is something that you've written and I've made a mess of. If not, I apologize to whoever did and not giving them proper credit. BTW Howard, I'm using 4.22.

Code:
 
$null = SetOption ("NoVarsInStrings","On")
$null = SetOption ("Explicit","On")
Dim $file,$filehandle,$namevalue,$line,$name,$namearray,$null,$outputfile,$outputhandle
$file = 'c:\kix\test_text.txt'
$filehandle = FreeFileHandle()
If Open ($filehandle,$file) = 0
$namevalue = 0
While @Error = 0
$line = ReadLine ($filehandle)
If InStr ($line,'DCS Application Snapshot')
ReDim Preserve $namearray[$namevalue]
$namearray[$namevalue] = $line
$namevalue = $namevalue+1
EndIf
If InStr ($line,'Authorization')
ReDim Preserve $namearray[$namevalue]
$namearray[$namevalue] = $line
$namevalue = $namevalue+1
EndIf
If InStr ($line,'Inbound communication address')
ReDim Preserve $namearray[$namevalue]
$namearray[$namevalue] = $line
$namevalue = $namevalue+1
EndIf
If InStr ($line,'Outbound communication address')
ReDim Preserve $namearray[$namevalue]
$namearray[$namevalue] = $line
$namevalue = $namevalue+1
EndIf
If InStr ($line,'Application name')
ReDim Preserve $namearray[$namevalue]
$namearray[$namevalue] = $line
$namevalue = $namevalue+1
EndIf
If InStr ($line,'Application status')
ReDim Preserve $namearray[$namevalue]
$namearray[$namevalue] = $line
$namevalue = $namevalue+1
EndIf
Loop
$null = Close ($filehandle)
$outputfile = 'c:\kix\test_text_out.txt'
$outputhandle = FreeFileHandle()
If Exist ($outputfile)
Del $outputfile
EndIf
If $namearray[0] <> ''
If Open ($outputhandle,$outputfile,5) = 0
For Each $name In $namearray
$null = WriteLine ($outputhandle,$name+@CRLF)
Next
$null = Close ($outputhandle)
Else
$null = MessageBox ('Unable To Create '+$outputfile,'Error',0)
EndIf
EndIf
Else
$null = MessageBox ('Unable To Open '+$file,'Error',0)
EndIf



_________________________
I could have made a neat retort but didn't, for I was flurried and didn't think of it till I was downstairs.
-Mark Twain

Top
#122584 - 2004-07-11 09:15 PM Re: Strip leading spaces?
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
I don't recognize that code as mine and if I did I would deny it. :P

Instead of all those IF constructs, a SELECT CASE construct would be more efficient since they are mutually exclusive. As for the Trim(), I misconstrued what you are trying to do. It looks like you want the entire line but without the leading spaces. If that is the case, LTrim() as Howard suggested is all you need.

Here is your code with some small changes:
Code:
break on

$null = SetOption ("NoVarsInStrings","On")
$null = SetOption ("Explicit","On")
Dim $file,$filehandle,$namevalue,$line,$name,$namearray,$null,$outputfile,$outputhandle
$file = 'c:\kix\test_text.txt'
$filehandle = FreeFileHandle()
If Open ($filehandle,$file) = 0
$namevalue = 0
While @Error = 0
$line = ReadLine ($filehandle)
Select
Case InStr ($line,'DCS Application Snapshot')
ReDim Preserve $namearray[$namevalue]
$namearray[$namevalue] = LTrim($line)
$namevalue = $namevalue+1

Case InStr ($line,'Authorization')
ReDim Preserve $namearray[$namevalue]
$namearray[$namevalue] = LTrim($line)
$namevalue = $namevalue+1

Case InStr ($line,'Inbound communication address')
ReDim Preserve $namearray[$namevalue]
$namearray[$namevalue] = LTrim($line)
$namevalue = $namevalue+1

Case InStr ($line,'Outbound communication address')
ReDim Preserve $namearray[$namevalue]
$namearray[$namevalue] = LTrim($line)
$namevalue = $namevalue+1

Case InStr ($line,'Application name')
ReDim Preserve $namearray[$namevalue]
$namearray[$namevalue] = LTrim($line)
$namevalue = $namevalue+1

Case InStr ($line,'Application status')
ReDim Preserve $namearray[$namevalue]
$namearray[$namevalue] = LTrim($line)
$namevalue = $namevalue+1
EndSelect
Loop
$null = Close ($filehandle)
$outputfile = 'c:\kix\test_text_out.txt'
$outputhandle = FreeFileHandle()
If Exist ($outputfile)
Del $outputfile
EndIf
If $namearray[0] <> ''
If Open ($outputhandle,$outputfile,5) = 0
For Each $name In $namearray
$null = WriteLine ($outputhandle,$name+@CRLF)
Next
$null = Close ($outputhandle)
Else
$null = MessageBox ('Unable To Create '+$outputfile,'Error',0)
EndIf
EndIf
Else
$null = MessageBox ('Unable To Open '+$file,'Error',0)
EndIf




I would rethink your loop however. There is an inherent flaw with looping on @Error=0 when you perform other functions within the loop that can throw @Error. It can end your loop pre-maturely. By readning in the file into an array first and then looping thru the array, it is mitigated.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#122585 - 2004-07-12 09:41 AM Re: Strip leading spaces?
brewdude6 Offline
Hey THIS is FUN

Registered: 2000-10-21
Posts: 280
Loc: Nashville, TN
Thank you for the help. Could I go on to strip out empty spaces from within the content of the returned string, or would that need to be structured totally different than the direction I'm approaching this?
_________________________
I could have made a neat retort but didn't, for I was flurried and didn't think of it till I was downstairs.
-Mark Twain

Top
#122586 - 2004-07-12 02:50 PM Re: Strip leading spaces?
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
e.g.
Code:
 'Authorization ID                         ='   


The approach would differ depending on whether you want the spaces between the last word and the equals:
'Authorization ID='
or to include the spaces between words:
'AuthorizationID='



Edited by Les (2004-07-12 03:05 PM)
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#122587 - 2004-07-12 03:01 PM Re: Strip leading spaces?
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Not tested ...

I have not verified that

$null = SetOption ("NoVarsInStrings","On")
$null = SetOption ("Explicit","On")

are followed in the older UDFs for this example.

Code:
break on


$null = SetOption ("NoVarsInStrings","On")
$null = SetOption ("Explicit","On")
Dim $file,$filehandle,$namevalue,$line,$name,$namearray,$null,$outputfile,$outputhandle
$file = 'c:\kix\test_text.txt'
$filehandle = FreeFileHandle()
If Open ($filehandle,$file) = 0
$namevalue = 0
Dim $aText, $text, $sMatch
$aText = "DCS Application Snapshot",
"Authorization",
"Inbound communication address",
"Outbound communication address",
"Application name",
"Application status"

$line = ReadLine ($filehandle)
While @Error = 0
for each $text in $aText
$sMatch = "(" + $text + ").*?= (.*?)$"

if RegExpTest($sMatch, $line, 1)
$line = RegExpReplace($line, "^\s+|\s+$", "", 1, 1)
$line = RegExpReplace($line, "\s+=\s+", "=", 1, 1)
Writelog2('c:\kix\test_text_out.txt', $line)
else
? "-No Match found"
endif
Next
$line = ReadLine ($filehandle)
Loop
Endif


;----------- Supporting UDFs ------------
Function RegExpTest($Pattern, $String, $IgnoreCase)

Dim $regEx, $Match, $Matches ; Create variable.

$regEx = createobject("VBscript.RegExp") ; Create a regular expression.

$regEx.Pattern = $Pattern ; Set pattern.
$regEx.IgnoreCase = val($IgnoreCase) ; Set case insensitivity.

$RegExpTest = $regEx.Test($String) ; Execute test search.
EndFunction

Function RegExpReplace($String1, $Pattern, $String2, $IgnoreCase, $Global)

Dim $regEx ; Create variable.
Dim $Results[0]
$regEx = createobject("VBscript.RegExp") ; Create a regular expression.

$regEx.Pattern = $Pattern ; Set pattern.
$regEx.IgnoreCase = val($IgnoreCase) ; Set case insensitivity.
$regEx.Global = val($Global) ; Set global applicability.

$RegExpReplace = $regEx.Replace($String1, $String2) ; Execute search.
EndFunction

Function WriteLog2($File, $Text, optional $TimeStamp)
dim $RC, $FH, $nul

$FH = FreeFileHandle()
if $FH > 0
$RC=Open ($FH, $File, 5)
Select
Case $RC=0
if ($TimeStamp=1)
$TimeStamp = @Date + " " + @Time + " - "
else
$TimeStamp = ""
endif
$RC=Writeline ($FH, $TimeStamp + $Text + @CRLF)
if ($RC <> 0)
$text = "WriteLine error: " + $RC + @CRLF
+ " -4 = File not open for writing" + @CRLF
+ " -3 = File number not open" + @CRLF
+ " -2 = Invalid file number specified" + @CRLF
+ " -1 = End of file"
$nul = MessageBox ($text,"Script Error",48)
endif
$nul=Close ($FH)
exit $RC
Case $RC=-2
$text = "WriteLog2: Invalid file handle (" + $FH + ") specified when trying to Open " + $File
$nul = MessageBox ($text,"Script Error",48)

Case $RC=-1
$text = "WriteLog2: Invalid file name (" + $File + ") specified for log file."
$nul = MessageBox ($text,"Script Error",48)

Case $RC=>0
$text = "System Error(" + $RC + ") while attempting to open log file (" + $File +")."
$nul = MessageBox ($text,"Script Error",48)
Endselect

else
$text = "WriteLog2: No free file handles for opening " + $File
$nul = MessageBox ($text,"Script Error",48)
$RC = -2
endif
exit $RC
EndFunction




Edited by Howard Bullock (2004-07-12 09:11 PM)
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#122588 - 2004-07-12 08:59 PM Re: Strip leading spaces?
brewdude6 Offline
Hey THIS is FUN

Registered: 2000-10-21
Posts: 280
Loc: Nashville, TN
Thank you for taking the time to look at this. I changed "foreach" and added @text to DIM, but it error's on line 29. To be honest with you, this is beyond my skills and I can't follow the code in that line. Thanks again.
_________________________
I could have made a neat retort but didn't, for I was flurried and didn't think of it till I was downstairs.
-Mark Twain

Top
#122589 - 2004-07-12 09:08 PM Re: Strip leading spaces?
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
You did not answer my question. To take out extra spaces, you can Join(Split())
Code:
         $namearray[$namevalue] = Join(Split(LTrim($line),'  '),'')

_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#122590 - 2004-07-12 09:12 PM Re: Strip leading spaces?
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
I have edited the previous post. I found an extra ")" on line 25. I also fixed the two items you listed.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#122591 - 2004-07-13 12:55 AM Re: Strip leading spaces?
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11631
Loc: CA
Try searching in the UDF forum or the mirror sites. I could sware there was a UDF to do this already.
Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 484 anonymous users online.
Newest Members
Sir_Barrington, batdk82, StuTheCoder, M_Moore, BeeEm
17886 Registered Users

Generated in 0.142 seconds in which 0.065 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org