Finding the displacement of a particular field in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding the displacement of a particular field in a file
# 1  
Old 03-05-2012
Finding the displacement of a particular field in a file

Hi

I have a file named Employee and it has the following format:
Code:
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:

Code:
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.
# 2  
Old 03-05-2012
Where are you pulling that data from? I can't see either of 1, 11, or 26 in the sample you provided.
# 3  
Old 03-05-2012
Quote:
Originally Posted by pludi
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
# 4  
Old 03-05-2012
I can sort of see...

How to determine though?
Is
Code:
NAME:010:C (1-10)
ACCOUNT NUM:015,00:N (11-25)
DATE:008,00: D (26-33)

in a file by itself?
# 5  
Old 03-05-2012
Quote:
Originally Posted by joeyg
How to determine though?
Is
Code:
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

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

where 010, 015 and 008 are the lengths of the particular fields.
# 6  
Old 03-05-2012
Hi bobby1015,

One way:
Code:
$ 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
This User Gave Thanks to birei For This Post:
# 7  
Old 03-06-2012
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?

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

Thanks ..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Finding difference in 1st field for rows of data

I have a file that has multiple lines, of grouped data, that typically all have the same values in the 1st field, however, I would like to search the 1st field for any differences and set a flag to use in an "if" statement to run some other routine. An example of the typical file is below,... (2 Replies)
Discussion started by: co21ss
2 Replies

2. Shell Programming and Scripting

Finding the length of a string in a field

I have a field MPN-BANK-NUMBERO:006,00:N in a file FILE1 How can i display the length of the Bank Number.Any idea ? The output shud take the following format FILE 1 6 Where 1 is the starting position and 6 is the Ending position. (9 Replies)
Discussion started by: bobby1015
9 Replies

3. Shell Programming and Scripting

Help with Awk finding and replacing a field based on a condition

Hi everybody, I'm trying to replace the $98 field with "T" if the last field (108th) is T I've tried awk 'BEGIN{OFS=FS="|"} {if ($108=="T")sub($98,"T"); print}' test.txt but that doesn't do anything also tried awk 'BEGIN{OFS=FS="|"}{ /*T.$/ sub($98,"T")} { print}' test.txt but... (2 Replies)
Discussion started by: jghi123
2 Replies

4. Shell Programming and Scripting

Help finding a field value then printing line

Hello, I'm trying to only print out the file systems that are greater than 90% full. So far I've got: df -k >sawky8 cat sawky8 | grep -v Filesystem | sed "s/%//g;" >sawky9 cat sawky9 | awk '{print $4}' | read stot print $stot if ;then echo $LINE Problem is it stops after the first... (2 Replies)
Discussion started by: Grueben
2 Replies

5. Shell Programming and Scripting

Appending 1st field in a file into 2nd field in another file

Hi, I've internally searched through forums for about 2+ hours. Unfortunately, with no luck. Although I've found some cases close to mine below, but didn't help so much. Actually, I'm in short with time. So I had to post my case. Hoping that you can help. I have 2 files, FILE1 ... (0 Replies)
Discussion started by: amurib
0 Replies

6. Shell Programming and Scripting

finding field count escaping the blank values

Hi All I have a file.Below are few records of the file. sample.txt CPS,ES,843232910001,ESF81462,W N LINDSAY LTD,01674840629,09-FEB-2009,23-FEB-2009,CDR735,ALL CALLS,01674840629 CPS,ES,843232670001,ESF81462,W N LINDSAY LTD,01674840629,09-FEB-2009,23-FEB-2009,CDR734,ALL... (2 Replies)
Discussion started by: king007
2 Replies

7. Shell Programming and Scripting

awk command for finding field in a file

hi guys i have this file with column number 7 as below: 0 1416 49 37 5 3 2 0 0 0 21 0 26 ... (5 Replies)
Discussion started by: npatwardhan
5 Replies

8. Shell Programming and Scripting

Help with finding length of a field

I have a pipe delimited file. I need to check that the first and second fields are 5 characters long and if not i need to append 0 in front of them to make them 5 characters long. can some body let mwe know how i can find the length of the two fields and then make them 5 characters long if they... (6 Replies)
Discussion started by: dsravan
6 Replies

9. Shell Programming and Scripting

Finding unique reocrds at a particular field

I have a pipe delimited flat file. I want to grep the records that are unique in the 4th field and repeat only once in the file for e.g.. if the file contains this 3 records i want to get the o/p as: I just gave a sample here and the file is huge one and i cant just grep from the... (7 Replies)
Discussion started by: dsravan
7 Replies

10. Shell Programming and Scripting

Regarding displacement of certain string

Hello,ladies and gentlemen I want to displace a string with another different string in a Korn shell file(*.h) which will includes the code to achieve the goal.How should I code? For example, In receive.sh,I wanna use (Hi) to replace all the (hello).The code to achieve this must be... (2 Replies)
Discussion started by: joshuaduan
2 Replies
Login or Register to Ask a Question