Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Test Code to read data from file

by GrandFather (Saint)
on Mar 18, 2012 at 23:52 UTC ( [id://960348]=note: print w/replies, xml ) Need Help??


in reply to Test Code to read data from file

Your code expects fixed size records but the data you show consists of variable length lines. It is not at all clear what you are trying to achieve, unless you simply want the first 63 characters from each line. Ignoring most of the extra formatting code that could be just:

#!/usr/bin/perl use strict; use warnings; my $fileData = <<FILE; 19:39:44.765096 IP 10.195.32.212.49152 > 255.255.255.255.trivnet1: UDP +, length 12 19:39:44.765572 IP 10.195.32.212.49152 > 239.0.82.11.trivnet1: UDP, le +ngth 12 19:39:45.202568 IP 10.195.32.96.61804 > 255.255.255.255.sentinelsrm: U +DP, length 40 19:39:45.265116 IP 10.195.32.212.49152 > 255.255.255.255.trivnet1: UDP +, length 12 19:39:45.265590 IP 10.195.32.212.49152 > 239.0.82.11.trivnet1: UDP, le +ngth 12 19:39:45.411153 IP 10.195.32.198.netbios-ns > 10.195.32.255.netbios-ns +: NBT UDP PACKET(137): QUERY; REQUEST; BROADCAST 19:39:45.412136 IP 10.195.32.198.netbios-dgm > 10.195.32.255.netbios-d +gm: NBT UDP PACKET(138) 19:39:45.620442 STP 802.1d, Config, Flags [none], bridge-id 8020.00:22 +:56:29:3a:00.8014, length 43 19:39:45.765086 IP 10.195.32.212.49152 > 255.255.255.255.trivnet1: UDP +, length 12 FILE open my $fIn, '<', \$fileData; while (<$fIn>) { chomp; printf ">> $. %s\n", substr $_, 0, 63; }

Prints:

>> 1 19:39:44.765096 IP 10.195.32.212.49152 > 255.255.255.255.trivne >> 2 19:39:44.765572 IP 10.195.32.212.49152 > 239.0.82.11.trivnet1: >> 3 19:39:45.202568 IP 10.195.32.96.61804 > 255.255.255.255.sentine >> 4 19:39:45.265116 IP 10.195.32.212.49152 > 255.255.255.255.trivne >> 5 19:39:45.265590 IP 10.195.32.212.49152 > 239.0.82.11.trivnet1: >> 6 19:39:45.411153 IP 10.195.32.198.netbios-ns > 10.195.32.255.net >> 7 19:39:45.412136 IP 10.195.32.198.netbios-dgm > 10.195.32.255.ne >> 8 19:39:45.620442 STP 802.1d, Config, Flags [none], bridge-id 802 >> 9 19:39:45.765086 IP 10.195.32.212.49152 > 255.255.255.255.trivne
True laziness is hard work

Replies are listed 'Best First'.
Re^2: Test Code to read data from file
by 1hab (Initiate) on Mar 19, 2012 at 00:16 UTC
    Example: Time stamp Source IP address Destination IP address 10:10:00 10.10.12.2 10.10.12.20 Protocol TCP Thank you so much for your excelent and quick response. This is what I was trying to do, and that is to print four headers (ta +ble), then insert the information below each header. But since I cou +ld NOT do that, I tried and asked for help to print the whole line, t +his way I would have all infomation needed. I will keep working at i +t, but I would greatly appreciated any suggestions. regards, 1hab

      Generally ask about the real issue. You are likely to get a better solution and may learn more in the process. The following code uses a regular expression to extract the data I think you want based on the sample you have supplied. It may not do exactly what you require, but learn about regular expressions and alter the match as required.

      #!/usr/bin/perl use strict; use warnings; my $fileData = <<FILE; 19:39:44.765096 IP 10.195.32.212.49152 > 255.255.255.255.trivnet1: UDP +, length 12 19:39:44.765572 IP 10.195.32.212.49152 > 239.0.82.11.trivnet1: UDP, le +ngth 12 19:39:45.202568 IP 10.195.32.96.61804 > 255.255.255.255.sentinelsrm: U +DP, length 40 19:39:45.265116 IP 10.195.32.212.49152 > 255.255.255.255.trivnet1: UDP +, length 12 19:39:45.265590 IP 10.195.32.212.49152 > 239.0.82.11.trivnet1: UDP, le +ngth 12 19:39:45.411153 IP 10.195.32.198.netbios-ns > 10.195.32.255.netbios-ns +: NBT UDP PACKET(137): QUERY; REQUEST; BROADCAST 19:39:45.412136 IP 10.195.32.198.netbios-dgm > 10.195.32.255.netbios-d +gm: NBT UDP PACKET(138) 19:39:45.620442 STP 802.1d, Config, Flags [none], bridge-id 8020.00:22 +:56:29:3a:00.8014, length 43 19:39:45.765086 IP 10.195.32.212.49152 > 255.255.255.255.trivnet1: UDP +, length 12 FILE my @headRow = ('Time stamp', 'Source IP address', 'Destination IP addr +ess'); my $rowFormat = "%-17s %-22s %-22s\n"; open my $fIn, '<', \$fileData; printf $rowFormat, @headRow; while (<$fIn>) { my ($time, $from, $to) = /(.{8})\.\d+ IP ([\d.]+)\.\d+ > ([\d.]+)\ +./; next if ! defined $to; printf $rowFormat, $time, $from, $to; }

      Prints:

      Time stamp Source IP address Destination IP address 19:39:44 10.195.32.212 255.255.255.255 19:39:44 10.195.32.212 239.0.82.11 19:39:45 10.195.32.96 255.255.255.255 19:39:45 10.195.32.212 255.255.255.255 19:39:45 10.195.32.212 239.0.82.11 19:39:45 10.195.32.212 255.255.255.255
      True laziness is hard work

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://960348]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (9)
As of 2024-04-18 15:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found