in reply to Re: Re: Re: Re: reading lines
in thread reading lines

my file contains multiple ipconfig outputs.

Ah, that changes things a bit. Is there a distinct and consistent string pattern that separates one ipconfig output from another, such as a blank line, or a line of dashes?

If so, you can assign that string to "$/" (rather than leaving it with the default value of CRLF, or setting it to undef, as suggested by BrowserUK). This way, you read one whole ipconfig output set in a single iteration of while (<>). For example, if the successive records are separated by a single blank line (and blank lines never occur within a single ipconfig output record), then you could do like this (untested):

my %hostdata; { $/ = "\x0d\x0a\x0d\x0a"; # record separator is now a blank line while (<>) { # $_ now contains one whole ipconfig output next unless ( /Host Name.*?:\s*(.*)/ ); my $host = $1; @{$hostdata{$host}{dns}} = ( /\s(\d+\.\d+\.\d+\.\d+)\s/g ); ( $hostdata{$host}{typ} ) = ( /Node Type.*?:\s*(.*)/ ); } } # Now, keys %hostdata gives the list of host names from # all the ipconfig runs, and $hostdata{"hostname"} contains # the dns and node type info for each host. for ( sort keys %hostdata ) { print join "\n", $_, @{$hostdata{$_}{dns}}, $hostdata{$_}{typ}, "\n +"; }
(update: removed a spurious line of code)

If the separation between ipconfig runs is not distinct or consistent, then you'll want to fall back on BrowserUK's less-favored method that reads one line at a time and uses a status flag to keep track of what it's supposed to look for (and what it's supposed to do) as it works through each multi-line record.

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Re: reading lines
by spill (Novice) on Dec 07, 2003 at 18:13 UTC
    It didn't dawn on me that HOW I was reading the file would affect how I parsed it. Your example looks like what my end goal is. I want to output each host into csv format and putting them into hashes seem the most logical. Here is a FULL sample of my input file with two computers. I read this file from STDIN and output the results in csv format. Another problem in this data is multiple adapters. My code up to know has choked on more than one adapter.
    ipconfig start Windows IP Configuration Host Name . . . . . . . . . . . . : axter-win2003 Primary Dns Suffix . . . . . . . : Axter2.home Node Type . . . . . . . . . . . . : Hybrid IP Routing Enabled. . . . . . . . : No WINS Proxy Enabled. . . . . . . . : No DNS Suffix Search List. . . . . . : Axter2.home brnmll01.nj.comcast.net Ethernet adapter Local Area Connection: Connection-specific DNS Suffix . : brnmll01.nj.comcast.net Description . . . . . . . . . . . : SMC EtherPower II 10/100 Etherne +t Adapter Physical Address. . . . . . . . . : 00-E0-29-0A-F5-E2 DHCP Enabled. . . . . . . . . . . : Yes Autoconfiguration Enabled . . . . : Yes IP Address. . . . . . . . . . . . : 192.168.0.6 Subnet Mask . . . . . . . . . . . : 255.255.255.0 Default Gateway . . . . . . . . . : 192.168.0.1 DHCP Server . . . . . . . . . . . : 192.168.0.1 DNS Servers . . . . . . . . . . . : 127.0.0.1 192.168.0.1 Lease Obtained. . . . . . . . . . : Tuesday, September 16, 2003 6:18 +:13 PM Lease Expires . . . . . . . . . . : Friday, September 19, 2003 6:18: +13 PM ipconfig end ipconfig start Windows 98 IP Configuration Host Name . . . . . . . . . : LAPTOP.no.cox.net DNS Servers . . . . . . . . : 205.152.132.235 181.171.2.200 10.10.10.1 Node Type . . . . . . . . . : Broadcast NetBIOS Scope ID. . . . . . : IP Routing Enabled. . . . . : No WINS Proxy Enabled. . . . . : No NetBIOS Resolution Uses DNS : No 0 Ethernet adapter : Description . . . . . . . . : PPP Adapter. Physical Address. . . . . . : 44-45-53-54-00-00 DHCP Enabled. . . . . . . . : Yes IP Address. . . . . . . . . : 181.171.2.147 Subnet Mask . . . . . . . . : 255.255.0.0 Default Gateway . . . . . . : 181.171.2.147 DHCP Server . . . . . . . . : 255.255.255.255 Primary WINS Server . . . . : Secondary WINS Server . . . : Lease Obtained. . . . . . . : 01 01 80 12:00:00 AM Lease Expires . . . . . . . : 01 01 80 12:00:00 AM 1 Ethernet adapter : Description . . . . . . . . : PPP Adapter. Physical Address. . . . . . : 44-45-53-54-00-01 DHCP Enabled. . . . . . . . : Yes IP Address. . . . . . . . . : 0.0.0.0 Subnet Mask . . . . . . . . : 0.0.0.0 Default Gateway . . . . . . : DHCP Server . . . . . . . . : 255.255.255.255 Primary WINS Server . . . . : Secondary WINS Server . . . : Lease Obtained. . . . . . . : Lease Expires . . . . . . . : 2 Ethernet adapter : Description . . . . . . . . : D-Link AIRPLUS Wireless LAN Adapter Physical Address. . . . . . : 00-80-C8-B5-76-1F DHCP Enabled. . . . . . . . : Yes IP Address. . . . . . . . . : 10.10.10.104 Subnet Mask . . . . . . . . : 255.255.255.0 Default Gateway . . . . . . : 10.10.10.1 DHCP Server . . . . . . . . : 10.10.10.1 Primary WINS Server . . . . : Secondary WINS Server . . . : Lease Obtained. . . . . . . : 06 19 03 2:51:53 PM Lease Expires . . . . . . . : 06 26 03 2:51:53 PM ipconfig end
    It is clear that my data is more complicated than my first example. I thought it would be easier to understand what I needed to do, but it only introduced more confusion. ;) Thanks to everyone that has responded, if anything I have learned some other techniques for manipulating my data.