Extract text from a textfile

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
reyntjensw
Posts: 1
Joined: 17 Oct 2009 05:06

Extract text from a textfile

#1 Post by reyntjensw » 17 Oct 2009 05:13

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

SenHu
Posts: 19
Joined: 19 Mar 2009 14:57

Scipt to extract ip address

#2 Post by SenHu » 19 Oct 2009 08:50

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

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Extract text from a textfile

#3 Post by ghostmachine4 » 23 Oct 2009 01:19

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


Post Reply