Question : Reading/Writing Fixed Value Position files n PERL

Hi,
I am trying to read a file that has 6 colons. The data will show up at exactly at the same
colon positions. I tried to read up on seek and tell but didn't find a good explaination.
Johanne1

here is 3 lines of input file.
      743 Hafslund Telekom Nettjene 02011          20090616130000      20100208152959                     I
       704 Carrot Communications ASA 02011          20100208153000      99991231000000                     I
             729 TDC AS - 729              02090          20080609083000      99991231000000                     I

The first few colons are blank spaces and can be taken out.
There are 6 colons but only care about the first 5 colons, i.e. last colon can be taken out.
Desired output has 2 requirements;
1. separate the colons by some kind of delimiter example semi-colon.
2. colon 5 is a date if it starts by 9999 then replace with nothing if the date does not start
with 9999 then don't replace it.

desired output:

743;Hafslund Telekom Nettjene;02011;20090616130000;20100208152959;
704;Carrot Communications ASA;02011;20100208153000;;
729;TDC AS -729;02090;20080609083000;;


I wrote a script to decipher between words and letters and spaces but it does not
wrok because of colon 2 which is a name that can include the number.
The script below gives right answer for first 2 lines but line 3 is split incorrectly.

incorrect ouput for line 3:
729;TDC AS -;729;02090;20080609083000;;

The second 729 is part ot the name.

It was suggested to me to used fixed position since each data part will always appear in
the same colon.
Start counting from 0.
example: colon 1 starts in  7th position
               colon 2 starts in 12th postion
               colon 3 starts in 36th position
               colon 4 starts in 48th position
               colon 5 starts in 67th position.

Is there any way I can easily store the contents of each colonn using the colon position.

Here is my script based on spaces, decimals and words but will never work 'cause of
colon 2 contains a title that has words and might have a number.

$infile = "input.txt";
$outfile = "output.txt";
open (OUTF, ">$outfile") || die "Can not open $outfile";
open (INF, "<$infile") || die "Can not open $infile";
while(<INF>)
{
      $tmp = $_;
      chomp($tmp);
        $tmp=~ s/^\s+//;
      @arr = split (/\s+/, $tmp);
      $temp = "";
      for ($i = 0; $i <= $#arr; $i++)
      {
            if ($arr[$i] =~ /\d+/) {
                        if (($i eq ($#arr - 1)) && ($arr[$i]=~/^9999/))   {$temp = $temp."\;";}

                  else {      $temp = $temp.$arr[$i]."\;";}
            }
            elsif ($arr[$i+1] ne "")
            {
                  if ($arr[$i+1] =~ /\d+/) { $temp = $temp.$arr[$i]."\;";}
                  else { $temp = $temp.$arr[$i]." ";}
            }
      }
      print OUTF "$temp\n";
}
close(INF);
close(OUTF);

Thanks in advance for your advice.




the tricky part is the second colon that is a name that can have letters and numbers and
have several words

Answer : Reading/Writing Fixed Value Position files n PERL

You can use splitting on a char  level & splicing to do the trick... Just remember that the elements spliced from array are removed so shift your "pointer" accordingly. Test.txt looks like this in my case:
pat1 pattern pattern2
xxd1 pappape dfadafda
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
#!/bin/perl
#
use strict;
use warnings;


open IH,"<test.txt" or die "cannot open file: test.txt";
my @data =<IH>;
close IH;

foreach my $line (@data){
 my @spl = split (//,$line);
 my @col1 = splice @spl,0,4; #positions 2-9;
 my @col2 = splice @spl,1,7; #positions 2-9;
  
 print @col1; 
 print "\n";
 print @col2; 
 print "\n";
}
Random Solutions  
 
programming4us programming4us