Finding the displacement of a particular field in a file

Hi

I have a file named Employee and it has the following format:

File name:Employee
00000
0000:0000
YYNNN
20120302
NAME:010:C
ACCOUNT NUM:015,00:N
DATE:008,00:D
#DATA#
ROGER     00000002346123720120302
MAX        00000003654774020110612
JOHNSON  00000000000103420120713
#END#
0000003

How am going to get the displacements for a particular field say for example, I need to have the displacement of ACCOUNT NUM, the answer would be 11, because it starts at position 11. Likewise, I need to get the displacements of all fields in the following format:

Field               Displacement
Name                    1
Account Num         11
ID                        26

Do we have any command which can get the desired output as shown above. Thanks..Appreciated your help.

Where are you pulling that data from? I can't see either of 1, 11, or 26 in the sample you provided.

Sorry for making you confused. Actually the length of the field is being specified in the data itself. Please find it(BOLD) below:

NAME:010:C (1-10)
ACCOUNT NUM:015,00:N (11-25)
DATE:008,00: D (26-33)

Thanks

How to determine though?
Is

NAME:010:C (1-10)
ACCOUNT NUM:015,00:N (11-25)
DATE:008,00: D (26-33)

in a file by itself?

No, (1-10) (11-25) (26-33) is not included in the file. Just for making it more clear, I have put it in a file. The actual data in the file is

NAME:010:C 
ACCOUNT NUM:015,00:N 
DATE:008,00: D 
.
.
.
.

where 010, 015 and 008 are the lengths of the particular fields.

Hi bobby1015,

One way:

$ cat infile
00000
0000:0000
YYNNN
20120302
NAME:010:C
ACCOUNT NUM:015,00:N
DATE:008,00:D
#DATA#
ROGER     00000002346123720120302
MAX        00000003654774020110612
JOHNSON  00000000000103420120713
#END#
0000003
$ cat script.pl
use warnings;
use strict;

die qq[Usage: perl $0 <infile> \n] unless @ARGV == 1;

my $displacement = 1;

printf qq[%s\t\t%s\n], qq[Field], qq[Displacement];
while ( <> ) {
        my @f = split /[:,]/, $_;
        next unless @f >= 2;
        if ( $f[0] =~ m/\D/ && $f[1] =~ m/\A\d+\Z/ ) {
                printf qq[%s\t\t%s\n], $f[0], $displacement;
                $displacement += $f[1];
        }
}


$ perl script.pl infile
Field           Displacement
NAME            1
ACCOUNT NUM             11
DATE            26

Regards,
Birei

1 Like

Thanks a ton Birei..All i can say is a big WOWWWWWWWWW

---------- Post updated 03-06-12 at 09:26 AM ---------- Previous update was 03-05-12 at 09:49 AM ----------

Birei,

If I want to get the following output, How am I going to do it?

Field                     Displacement               Length 
NAME                          1                            10
ACCOUNT NUM             11                            15
DATE                          26                             8

Thanks ..

Try next script. Note that each field of output is separated with two tabs and seems weird. Change to your needs.

$ cat script.pl                                                                                                                                                                                                                              
use warnings;                                                                                                                                                                                                                                
use strict;                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                             
die qq[Usage: perl $0 <infile> \n] unless @ARGV == 1;                                                                                                                                                                                        
                                                                                                                                                                                                                                             
my $displacement = 1;                                                                                                                                                                                                                        
                                                                                                                                                                                                                                             
printf qq[%s\t\t%s\t\t%s\n], qw[Field Displacement Length];                                                                                                                                                                                  
while ( <> ) {                                                                                                                                                                                                                               
        my @f = split /[:,]/, $_;                                                                                                                                                                                                            
        next unless @f >= 2;                                                                                                                                                                                                                 
        if ( $f[0] =~ m/\D/ && $f[1] =~ m/\A\d+\Z/ ) {                                                                                                                                                                                       
                printf qq[%s\t\t%s\t\t%d\n], $f[0], $displacement, $f[1];                                                                                                                                                                    
                $displacement += $f[1];                                                                                                                                                                                                      
        }                                                                                                                                                                                                                                    
}                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                             
$ perl script.pl infile
Field           Displacement            Length                                                                                                                                                                                               
NAME            1               10                                                                                                                                                                                                           
ACCOUNT NUM             11              15                                                                                                                                                                                                   
DATE            26              8                                                                                                                                                                                                            
$

Thank you so much.. Yea the ouput seems to be weird. Which part of the code do I need to change to make it(output) more clear.

Birie,

Like this (changes in red):

$ cat script.pl
use warnings;
use strict;

die qq[Usage: perl $0 <infile> \n] unless @ARGV == 1;

my $displacement = 1;

printf qq[%-20s %-20s %s\n], qw[Field Displacement Length];
while ( <> ) { 
        my @f = split /[:,]/, $_; 
        next unless @f >= 2;
        if ( $f[0] =~ m/\D/ && $f[1] =~ m/\A\d+\Z/ ) { 
                printf qq[%-20s %-20s %d\n], $f[0], $displacement, $f[1];
                $displacement += $f[1];
        }   
}
$ perl script.pl infile                                                                                                                                                                                                                      
Field                Displacement         Length                                                                                                                                                                                             
NAME                 1                    10
ACCOUNT NUM          11                   15
DATE                 26                   8

Thanks a ton birei. Thats works well.
How can we output that to a csv file? Appreciated your help..

Try:

$ cat script.pl
use warnings;
use strict;

die qq[Usage: perl $0 <infile> \n] unless @ARGV == 1;

my $displacement = 1;

 printf qq[%s\n], join qq[,], qw[Field Displacement Length];
while ( <> ) {
        my @f = split /[:,]/, $_;
        next unless @f >= 2;
        if ( $f[0] =~ m/\D/ && $f[1] =~ m/\A\d+\Z/ ) {
                 printf qq[%s,%d\n], join( qq[,], $f[0], $displacement ),$f[1];
                $displacement += $f[1];
        }
}

$ perl script.pl infile
Field,Displacement,Length
NAME,1,10
ACCOUNT NUM,11,15
DATE,26,8
1 Like

Birei, you are just AWSUM..Thank you soooooooooooooooooo much..