Fixed Length records- Korne Shell Program.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Fixed Length records- Korne Shell Program.
# 1  
Old 04-10-2007
Fixed Length records- Korne Shell Program.

Hi,

I need some help regarding in writing a Korne shell script, in determining the fixed length records in a data file. We have already utility in place, which does this work. The Code for this is as below. In the below $1 is the parameter passed to the script, which is the data file name.

This script is working for the files which is having characters less than or equal to 10239.

But this script fails for the lengthy data files, where the characters in the file exceeds 10239.

I have got 3 files, which have characters exceeding 13,000 and less than 16,000.

Can anyone let me know, as how to overcome this problem.

======================================================
CHKLEN=`awk 'NR==1 {print length($0)}' $1`
if [ "$CHKLEN" != "" ]
then
awk "length != $CHKLEN { exit 1 }" $1
if [ $? -ne 0 ]
then
return 1
fi

# Add one for CRLF
let CHKLEN=CHKLEN+1
echo $CHKLEN
else
echo 0
fi
return 0
=======================================================

Thanks and Regards,

rajesh
# 2  
Old 04-11-2007
HI,
AWK has a limitation of the length of records.
That is if a single line exceeds certain length, (I think its 1400 if i am not wrong) then it will throw an error.
Check the version of AWK and if possible get a newer version of it.
Better try using perl which is best suited for this.

Thanks
Raghuram
# 3  
Old 04-11-2007
Quote:
Originally Posted by nrajesh_2009
Hi,

I need some help regarding in writing a Korne shell script, in determining the fixed length records in a data file. We have already utility in place, which does this work. The Code for this is as below. In the below $1 is the parameter passed to the script, which is the data file name.

This script is working for the files which is having characters less than or equal to 10239.

All versions of awk can handle files of any size (subject to memory limitations if you are storing large portions of the file in memory).

With some versions of awk, you could have a problem with long lines. Is that the problem? If so, use GNU awk (gawk).
Quote:
But this script fails for the lengthy data files, where the characters in the file exceeds 10239.

I have got 3 files, which have characters exceeding 13,000 and less than 16,000.

Can anyone let me know, as how to overcome this problem.

======================================================
CHKLEN=`awk 'NR==1 {print length($0)}' $1`
if [ "$CHKLEN" != "" ]
then
awk "length != $CHKLEN { exit 1 }" $1
if [ $? -ne 0 ]
then
return 1
fi

# Add one for CRLF
let CHKLEN=CHKLEN+1
echo $CHKLEN
else
echo 0
fi
return 0
=======================================================

You don't need to call awk twice:

Code:
awk '
   NR == 1 { len = length }
   len && len != length { error = 1; exit }
   END {
             if ( error == 0 ) print len
             else exit 1
          }
'

# 4  
Old 04-11-2007
Hi Johnson,

Can you please post the same equivalent code using gnu gawk?

awaiting your reply.

thanks,

rajesh
# 5  
Old 04-11-2007
Quote:
Originally Posted by nrajesh_2009
Hi Johnson,

Can you please post the same equivalent code using gnu gawk?

Code:
gawk '
   NR == 1 { len = length }
   len && len != length { error = 1; exit }
   END {
             if ( error == 0 ) print len
             else exit 1
          }
'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert variable length record to fixed length

Hi Team, I have an issue to split the file which is having special chracter(German Char) using awk command. I have a different length records in a file. I am separating the files based on the length using awk command. The command is working fine if the record is not having any... (7 Replies)
Discussion started by: Anthuvan
7 Replies

2. Shell Programming and Scripting

Concatenating fixed length lines in shell script

I have a peculiar file with record format like given below. Each line is wrapped to next lines after certain number of characters. I want to concatenate all wrapped lines into 1. Input:(wrapped after 10 columns) This is li ne1 This is li ne2 and this line is too lo ng Shortline ... (8 Replies)
Discussion started by: kmanyam
8 Replies

3. Shell Programming and Scripting

Unix sort for fixed length columns and records

I was trying to use the AIX 6.1 sort command to sort fixed-length data records, sorting by specific columns only. It took some time to figure out how to get it to work, so I wanted to share the solution. The sort man page wasn't much help, because it talks about field delimeters (default space... (1 Reply)
Discussion started by: CheeseHead1
1 Replies

4. Shell Programming and Scripting

changing a variable length text to a fixed length

Hi, Can anyone help with a effective solution ? I need to change a variable length text field (between 1 - 18 characters) to a fixed length text of 18 characters with the unused portion, at the end, filled with spaces. The text field is actually field 10 of a .csv file however I could cut... (7 Replies)
Discussion started by: dc18
7 Replies

5. Shell Programming and Scripting

Make variable length record a fixed length

Very, very new to unix scripting and have a unique situation. I have a file of records that contain 3 records types: (H)eader Records (D)etail Records (T)railer Records The Detail records are 82 bytes in length which is perfect. The Header and Trailer records sometimes are 82 bytes in... (3 Replies)
Discussion started by: jclanc8
3 Replies

6. Shell Programming and Scripting

awk to extract incorrect fixed length records

I have a number of unix text files containing fixed-length records (normal unix linefeed terminator) where I need to find odd records which are an incorrect length. The data is not validated and records can contain odd backslash characters and control characters which makes them awkward to process... (2 Replies)
Discussion started by: methyl
2 Replies

7. Shell Programming and Scripting

reading fixed length flat file and calling java code using shell scripting

I am new to shell scripting and I have to to the following I have a flat file with storename(lenth 20) , emailaddress(lenth 40), location(15). There is NO delimiters in that file. Like the following str00001.txt StoreName emailaddress location... (3 Replies)
Discussion started by: willywilly
3 Replies

8. UNIX for Dummies Questions & Answers

Convert a tab delimited/variable length file to fixed length file

Hi, all. I need to convert a file tab delimited/variable length file in AIX to a fixed lenght file delimited by spaces. This is the input file: 10200002<tab>US$ COM<tab>16/12/2008<tab>2,3775<tab>2,3783 19300978<tab>EURO<tab>16/12/2008<tab>3,28523<tab>3,28657 And this is the expected... (2 Replies)
Discussion started by: Everton_Silveir
2 Replies

9. UNIX for Dummies Questions & Answers

What the command to find out the record length of a fixed length file?

I want to find out the record length of a fixed length file? I forgot the command. Any body know? (9 Replies)
Discussion started by: tranq01
9 Replies

10. Shell Programming and Scripting

creating a fixed length output from a variable length input

Is there a command that sets a variable length? I have a input of a variable length field but my output for that field needs to be set to 32 char. Is there such a command? I am on a sun box running ksh Thanks (2 Replies)
Discussion started by: r1500
2 Replies
Login or Register to Ask a Question