01.13.08
Reading a file of fixed lengths..
Perl is great for file processing and as I write more code with it, i constantly find new things that I would like to do. The other day I was trying to process a file with fixed number of lines and here’s what I prototyped. Please feel free to improve on it. I am always game for enhancements.
Here’s the file to process. I want to grab 3 lines and place them in one line.
#!/usr/bin/perl
open F, outt ;
@file=<F>
$n=0; $e=3;
for (@file) {
($x,$y,$z) = @file[$n..$e];
$x =~ s/\n//; $y =~ s/\n//; $z =~ s/\n//;
last if (!defined($x)) ;
print “LINE: \$y-[$y];\$x-[$x];\$z-[$z]\n”;
$n+=3; $e+=3;
}
[iosakwe@MackSTA]-0-[Sun Jan 13,06:37 PM]
<~/tmp> $ cat outt
adm-as01.ny.itginc.com
SunOS
root:BeYwy678Hg0xk:::::::
adm-as02.ny.itginc.com
SunOS
root:Aon/giarB00vM:6445::::::
adm-as03.ny.itginc.com
Linux
root:BeYwy678Hg0xk:12598:0:99999:7:::
[iosakwe@MackSTA]-0-[Sun Jan 13,06:37 PM]
<~/tmp> $ perl p.pl
LINE: $y-[SunOS];$x-[adm-as01.ny.itginc.com];$z-[root:BeYwy678Hg0xk:::::::]
LINE: $y-[SunOS];$x-[adm-as02.ny.itginc.com];$z-[root:Aon/giarB00vM:6445::::::]
LINE: $y-[Linux];$x-[adm-as03.ny.itginc.com];$z-[root:BeYwy678Hg0xk:12598:0:99999:7:::]