how to find lenght of fixed width file record? | Unix Linux Forums | UNIX for Advanced & Expert Users

  Go Back    


UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

how to find lenght of fixed width file record?

UNIX for Advanced & Expert Users


Tags
awk, fixed width, fixed width lenght

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-04-2012
Seshendranath Seshendranath is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 8 May 2013, 5:05 AM EDT
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Question how to find lenght of fixed width file record?

actually i am trying to find the lenght of fixed width file record reading from teradata db but its not working can u guys help me out?

code which i wrote---


Code:
colmn_lngth=`cat $RPT_FILE | awk -F~ '{print $1}'`
 
 rm $RPT_FILE
 
 
 while read line
 do
 
  result=`echo $line | wc -m`
  result=`expr $result - 1`
    
   if [ $result -le $colmn_lngth ]; then
        echo $line >> $Src_File.temp
   else
       echo $line >> $Src_File.bad
   fi
 done < $Src_File


but in file if record is having spaces in between then its not counting tht spaces so it is creating wrong validation.
and writing tht record in to file also its not going properly ....plzs guys can any help out with this issue.plzs

Last edited by Scott; 10-04-2012 at 09:55 AM.. Reason: Code tags
Sponsored Links
    #2  
Old 10-04-2012
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 14 April 2014, 6:10 AM EDT
Posts: 1,640
Thanks: 58
Thanked 476 Times in 472 Posts
is this fix width right..?

try

Code:
awk '{ print (length($0))}' file

if it is same for all the rows..
use..

Code:
awk 'END{ print (length($0))}' file

Sponsored Links
    #3  
Old 10-04-2012
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 23 April 2014, 8:57 AM EDT
Location: NM
Posts: 10,066
Thanks: 253
Thanked 760 Times in 714 Posts
'fixed width records' does not necessarily mean carriage control ("\n") at the end of a line. It just means the data are laid out in a fixed pattern, probably with no character to mark then end of the line. If it is a fixed record length file with carriage control, then just read the very first line.

Code:
awk '{print length($0); exit}' infile

Without carriage control you will have a much harder time finding the record length, generally you need to get that information from a source external to the file: file metadata, specifications, someone who knows.
    #4  
Old 10-04-2012
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 17 April 2014, 5:28 PM EDT
Location: Houston, Texas, USA
Posts: 660
Thanks: 0
Thanked 200 Times in 192 Posts

Code:
echo 123123123 | read var
echo ${#var}
9

Sponsored Links
    #5  
Old 10-05-2012
Seshendranath Seshendranath is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 8 May 2013, 5:05 AM EDT
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
actually wht ever command which i gave is working in intractive mode but when i run d program it removing spaces in record and returning d record count d same while writing in to file.
thaxz fr reply in advance.

---------- Post updated at 10:51 AM ---------- Previous update was at 08:55 AM ----------

can any give me code fr d below
Actually i am reading a record from teradata db table i.e, fixed width with spaces in between, now i want to find good record from tht file according to record length if length is more than the fixed width file length then it will remove tht error record to new file from base file. we will give file as parameter. Plzs help out in this
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to find lenght of fixed width file record? Seshendranath UNIX for Dummies Questions & Answers 0 10-04-2012 07:51 AM
Removing \n within a fixed width record CKT_newbie88 Shell Programming and Scripting 10 05-01-2009 03:21 PM
Can we convert a '|' file into a fixed lenght??? kumarsaravana_s Shell Programming and Scripting 2 05-30-2007 04:06 AM
How to insert a record in fixed width flatfile limou Shell Programming and Scripting 1 03-13-2006 03:55 PM
Fixed Width file using AWK alok.benjwal UNIX for Dummies Questions & Answers 2 12-05-2005 10:39 AM



All times are GMT -4. The time now is 07:29 AM.