Length of a fixed width file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Length of a fixed width file
# 1  
Old 06-19-2013
Length of a fixed width file

I have a fixed width file of length 53. when is try to get the lengh of the record of that file i get 2 different answers.

Code:
 
awk '{print length;exit}' <File_name>

The above code gives me length 50.


Code:
 
wc -L <File_name>

The above code gives me length 53.

Please clarify on the above.
# 2  
Old 06-19-2013
Amrutha24,

Code:
wc -L 

-L, --max-line-length
              print the length of the longest line

and the awk code you are using are not the same. This code will print the length of the first line only.
Code:
awk '{print length;exit}'





To print the max length of any line of the file you can use this with awk :

Code:
awk '{if(max=="") {max=length($0)};if(length($0)>max){max=length($0)}}END{print max}'  file

# 3  
Old 06-19-2013
Quote:
Originally Posted by Amrutha24
I have a fixed width file of length 53. when is try to get the lengh of the record of that file i get 2 different answers.

Code:
 
awk '{print length;exit}' <File_name>

The above code gives me length 50.

Code:
 
wc -L <File_name>

The above code gives me length 53.

Please clarify on the above.
According to the standards, the awk command given should print the number of characters in the 1st line in the file (not counting the trailing newline). But, some buggy versions (including Mac OS X's awk) of awk print the number of bytes instead of the number of characters.

The meaning of wc -L is not defined by the standards and the man pages I've seen that include this options say that -L gives the length of the longest line, but does not specify the units (bytes, characters, etc.), but with wc i'd expect that the count includes the terminating newline character.

When you say you have a fixed width file of length 53, it isn't clear whether you mean each line in the file is 53 bytes, 53 characters, 53 32-bit integers, etc., or a file that is a single line of 53 bytes, characters, ... If your input is encoded using UTF-8, 53 characters could take 53 to 313 bytes. Without knowing the actual contents of your input file, the encoding used to store characters in the file, what characters are contained in the file, the meaning of wc's -L option on your system, whether awk's length on your system counts bytes of characters, and what locale is being used when running the above commands; there isn't much we can do to diagnose what is going on in your specific case.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Alter Fixed Width File

Thank u so much .Its working fine as expected. ---------- Post updated at 03:41 PM ---------- Previous update was at 01:46 PM ---------- I need one more help. I have another file(fixed length) that will get negative value (ex:-00000000003000) in postion (98 - 112) then i have to... (6 Replies)
Discussion started by: vinus
6 Replies

2. Shell Programming and Scripting

Comparing two fixed width file

Hi Guys I am checking the treads to get the answer but i am not able to get the answer for my question. I have two files. First file is a pattern file and the second file is the file i want to search in it. Output will be the lines from file2. File1: P2797f12af 44751228... (10 Replies)
Discussion started by: anshul_er
10 Replies

3. Shell Programming and Scripting

Manupulating Records in a fixed width file

I am trying to determine what would be a fast and simple way to manipulate data that comes in a fixed width format. This data has 6 segments within a record. Each record needs to written out with a header and the 6 segments. Based on the value in column #6 the fields will be defined accordingly.... (4 Replies)
Discussion started by: Muga801
4 Replies

4. Shell Programming and Scripting

Fixed-Width file from Oracle

Hi All, I have created a script which generates FIXED-WIDTH file by executing Oracle query. SELECT RPAD(NVL(col1,CHR(9)),20)||NVL(col2,CHR(9))||NVL(col3,CHR(9) FROM XYZ It generates the data file with proper alignment. But if same file i transfer to windows server or Mainframe... (5 Replies)
Discussion started by: Amit.Sagpariya
5 Replies

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

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

7. Shell Programming and Scripting

Comparing column of variable length anf fixed width file

Hi, I have two input files. File1: ID Name Place 1-234~name1~Newyork 1-34~name2~Boston 1-2345~name3~Hungary File1 is a variable length file where each column is seperated by delimitter "~". File2: ID Country 1-34<<11 SPACES>>USA<<7 spaces>> 1-234<<10 SPACES>>UK<<8... (5 Replies)
Discussion started by: manneni prakash
5 Replies

8. Shell Programming and Scripting

Combining Two fixed width columns to a variable length file

Hi, I have two files. File1: File1 contains two fixed width columns ID of 15 characters length and Name is of 100 characters length. ID Name 1-43<<11 spaces>>Swapna<<94 spaces>> 1-234<<10 spaces>>Mani<<96 spaces>> 1-3456<<9 spaces>>Kapil<<95 spaces>> File2: ... (4 Replies)
Discussion started by: manneni prakash
4 Replies

9. UNIX Desktop Questions & Answers

Help with Fixed width File Parsing

I am trying to parse a Fixed width file with data as below. I am trying to assign column values from each record to variables. When I parse the data, the spaces in all coumns are dropped. I would like to retain the spaces as part of the dat stored in the variables. Any help is appreciated. I... (4 Replies)
Discussion started by: sate911
4 Replies

10. UNIX for Dummies Questions & Answers

Fixed Width file using AWK

I am using the following command at the Unix prompt to make my 'infile' into a fixed width file of 100 characters. awk '{printf "%-100s\n",$0}' infile > outfile However, there are some records with a special character "©" These records are using 3 characters in place of one and my record... (2 Replies)
Discussion started by: alok.benjwal
2 Replies
Login or Register to Ask a Question