Page 1 of 1

Extract text from a textfile

Posted: 17 Oct 2009 05:13
by reyntjensw
Hi,

I'm trying to extract a piece of text from a line, but I'm not getting there.

Code: Select all

Minimum Host   : 192.168.1.1         11000000.10101000.00000001.00000001     
Minimum Host   : 192.168.1.1         11000000.10101000.00000001.00000001     
Minimum Host   : 192.168.1.9         11000000.10101000.00000001.00001001     


So what I'd like to do is to get the ip address out of the line of text.

Code: Select all

set fso = wscript.createobject("Scripting.filesystemobject")

Set fileStream = fso.OpenTextFile("ip.txt", 1)

dim arrLijnen()
redim arrLijnen(1)
dim teller
do

   templine = filestream.readline

   arrLijn = split(templine,":")

   arrLijnen(teller) = arrLijn

   teller = teller + 1

   redim preserve arrLijnen(teller)
loop until filestream.atendofstream = true

filestream.close


So could anyone tell me how I could fix this?

Thanks

Scipt to extract ip address

Posted: 19 Oct 2009 08:50
by SenHu

Code: Select all

# Script ip.txt
var str text ; cat "C:/folder/file.txt" > $text
while ( { sen -r -c "^Host&\n^" $text } > 0 )
do
    var str ip ; stex -r -c "^Host&\n^" $text > $ip
    stex -r "]^(0>9)^" $ip > null ; stex -r "^(0>9)^l]" $ip
done



This is in biterscripting ( http://www.biterscripting.com ). Prints all ip addresses found in the file - one per line. Save the script as C:/Scripts/ip.txt, start biterscripting, enter the following command.

Code: Select all

script " C:/Scripts/ip.txt"


Use the correct path to input file "C:/folder/file.txt". Use double quotes.

Sen

Re: Extract text from a textfile

Posted: 23 Oct 2009 01:19
by ghostmachine4
reyntjensw wrote:Hi,

I'm trying to extract a piece of text from a line, but I'm not getting there.

Code: Select all

Minimum Host   : 192.168.1.1         11000000.10101000.00000001.00000001     
Minimum Host   : 192.168.1.1         11000000.10101000.00000001.00000001     
Minimum Host   : 192.168.1.9         11000000.10101000.00000001.00001001     


So what I'd like to do is to get the ip address out of the line of text.

Code: Select all

set fso = wscript.createobject("Scripting.filesystemobject")

Set fileStream = fso.OpenTextFile("ip.txt", 1)

dim arrLijnen()
redim arrLijnen(1)
dim teller
do

   templine = filestream.readline

   arrLijn = split(templine,":")

   arrLijnen(teller) = arrLijn

   teller = teller + 1

   redim preserve arrLijnen(teller)
loop until filestream.atendofstream = true

filestream.close


So could anyone tell me how I could fix this?

Thanks

after you split by ":" , split arrLijn(1) by "space" or "tab" depending on how your data is formatted. Then the first element of that split will be your ipaddress........

another simple way: if you can download gawk for windows http://gnuwin32.sourceforge.net/packages/gawk.htm: here's a one liner

Code: Select all

C:\test>gawk "{print $4}" file
192.168.1.1
192.168.1.1
192.168.1.9