**DONOTDELETE**
(Lurker)
2001-04-17 02:29 PM
Looking for tabs

I have a file that conatins data in the form
<data1> (tab) <data2> (tab) <data3>
The data in the middle field is IP addresses and so I cannot read the data with the substr command as they are different lengths. Is anyone aware of a way to strip the information by searching for the data between the tabs. I think the pseudocode for this would be something like
$data1 = (anything from start of line to tab)
$data2 = (data between tab1 + tab2)
$data3 = (anything from tab2 to the end of the line)

Thanks a lot

Chris


**DONOTDELETE**
(Lurker)
2001-04-17 03:01 PM
Re: Looking for tabs

Try using the instr() function to search for chr(9), the tab character. I haven't tested this but try something like:
code:

$temp=substr("$data1",instr("$data1",chr(9))+1,999)
$ptr=instr("$temp",chr(9))
$data3=substr("$temp",$ptr+1,999)
$data2=substr("$temp",1,$ptr-1)



**DONOTDELETE**
(Lurker)
2001-04-17 03:04 PM
Re: Looking for tabs

Oops. I used $data1 as the starting point instead of evaluating it but I'm sure you get the idea.

cj
(MM club member)
2001-04-17 03:04 PM
Re: Looking for tabs

TAB is CHR(9)

INSTR($text, chr(9)) will return the first occurrence of TAB in $text.

If you want to remove all the TABS by converting them to space try this:

piece of Swap.KiX

code:

$changefrom=chr(9)
$changeto=" "


while instr("$string", $changefrom)<>0
$string=substr($string, 1, instr("$string", $changefrom)-1) + $changeto + substr($string, instr("$string", $changefrom)+1, len($string))
loop


$string is your text
$changefrom is TAB
$changeto is SPACE

This will continue looking at string for TAB until there are none. If it finds a TAB, however, it uses the SUBSTR and INSTR functions to break the string apart and re-join it with the TAB missing and replaced with a SPACE.

If you just want to strip out the TABs, make $changeto=""

If you want to separate the pieces out without the TABs, you have two choices.

1. Use the SPLIT function in the new KiXtart 2001 Beta.

2. Use cjs VBSplit.KiX script that works in KiXtart 3.6x and does the same as the new KiX2k function.

You can get VBSplit.KiX from here


cj

JochenAdministrator
(KiX Supporter)
2001-04-17 03:52 PM
Re: Looking for tabs

Hiya Chris,

i can't resist to post my result (costs that much time !) alltough that vbsplit might be much better ..

so here's my sample text input :

code:

Machine1 10.139.123.20 Windows NT
Machine2 53.113.5.67 Windows 95
Machine3 10.128.12.15 Windows 2000


and here comes the script :

code:

Break ON CLS

$file = "PATH\test.txt"

DIM $name[3]
DIM $ip[3]
DIM $os[3]
$y = 0

$nul = open(1,$file,2)
$x = readline(1)
WHILE @error = 0
$name[$y] = substr($x,1,instr($x,chr(9))-1)
$x = substr($x,len($name[$y])+2,len($x)-instr($x,chr(9)))
$ip[$y] = substr($x,1,instr($x,chr(9))-1)
$os[$y] = substr($x,instr($x,chr(9))+1,len($x)-instr($x,chr(9)))
at($y,1)"-" + $name[$y] + "-"
at($y,25)"-" + $ip[$y] + "-"
at($y,50)"-" + $os[$y] + "-"
$x = readline(1)
$y = $y + 1
LOOP

$nul = close(1)


Get $k



Jochen

[This message has been edited by jpols (edited 17 April 2001).]

JochenAdministrator
(KiX Supporter)
2001-04-17 04:13 PM
Re: Looking for tabs

... or much better , make the array independant from the number of Lines :

code:

Break ON CLS
$file = "PATH\test.txt"
$nul = open(1,$file,2)
$x = readline(1)
while @error = 0
$y = $y + 1
$x = readline(1)
loop
$nul = close(1)
DIM $name[$y]
DIM $ip[$y]
DIM $os[$y]
$y = 0
$nul = open(1,$file,2)
$x = readline(1)
WHILE @error = 0
$name[$y] = substr($x,1,instr($x,chr(9))-1)
$x = substr($x,len($name[$y])+2,len($x)-instr($x,chr(9)))
$ip[$y] = substr($x,1,instr($x,chr(9))-1)
$os[$y] = substr($x,instr($x,chr(9))+1,len($x)-instr($x,chr(9)))
at($y,1)"-" + $name[$y] + "-"
at($y,25)"-" + $ip[$y] + "-"
at($y,50)"-" + $os[$y] + "-"
$x = readline(1)
$y = $y + 1
LOOP
$nul = close(1)
Get $k


Jochen

[This message has been edited by jpols (edited 18 April 2001).]