Sponsored Content
Top Forums Shell Programming and Scripting Finding the displacement of a particular field in a file Post 302604905 by birei on Tuesday 6th of March 2012 09:37:52 AM
Old 03-06-2012
Try next script. Note that each field of output is separated with two tabs and seems weird. Change to your needs.
Code:
$ 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                                                                                                                                                                                                            
$

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 11:11 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy