how to fix the column length in a file using Awk Prog

Hi

I use the following code to read the file and to fix the length of the column of the record in the file 'Sample.txt'

ls Samp* | awk '
{ a[NR]=$1 }
END{
FS="n"
for(i=1;i<=NR;i++)
{
while( getline < a ) 
{
f1=$0;
print("Line::",f1); 
f2=substr(f1,1,10) 
print("Field1::",f2);
f3=substr(f1,11,57)
print("Field2::",f3);
f5=substr(f1,58,75)
print("Field4::",f5);
f6=substr(f1,86,96) 
print("Field5::",f6);
f7=substr(f1,97,104)
print("Field6::",f7);
}
}

Contents of Sample.txt

1234567891XYZABC                            13/08/09 00:00:00 67890   12345 67890       00

but only field1 is printed correctly as below

Line::1234567891XYZABC                            13/08/09 00:00:00 67890   12345 67890       00
Field1::1234567891
Field2::XYZABC                                     13/08/09 00:00:00
Field3::13/08/09 00:00:00          67890   12345 67890       00

Please input your thougts to get the fields correctly...

Thanks for your Inputs
meva

To keep the forums high quality for all users, please take the time to format your posts correctly.

  1. Use Code Tags when you post any code or data samples so others can easily read your code.
    You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags and by hand.)
  2. Avoid adding color or different fonts and font size to your posts.
    Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.
  3. Be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
Reply With Quote

Thanks...Going forward i ll make sure that i dont miss the above rules.

Meva.

You don't understand :rolleyes: edit your first post and add the [code] tags.

##Code included with the code tags
Hi

I use the following code to read the file and to fix the length of the column of the record in the file 'Sample.txt'

ls Samp* | awk '
{ a[NR]=$1 }
END{
FS="n"
for(i=1;i<=NR;i++)
{
while( getline < a ) 
{
f1=$0;
print("Line::",f1); 
f2=substr(f1,1,10) 
print("Field1::",f2);
f3=substr(f1,11,57)
print("Field2::",f3);
f5=substr(f1,58,75)
print("Field4::",f5);
f6=substr(f1,86,96) 
print("Field5::",f6);
f7=substr(f1,97,104)
print("Field6::",f7);
}
}

Contents of Sample.txt

1234567891XYZABC 13/08/09 00:00:00 67890 12345 67890 00

OUTPUT
but only field1 is printed correctly as below

Line::1234567891XYZABC 13/08/09 00:00:00 67890 12345 67890 00
Field1::1234567891
Field2::XYZABC 13/08/09 00:00:00
Field3::13/08/09 00:00:00 67890 12345 67890 00

Please input your thougts to get the fields correctly...

Thanks for your Inputs
meva

And what is the correct/required output ?

Remember, you have to use [code] tags when you post sample data and required output.

The required OUTPUT is

awk '{
        l=$0
        a=int($1)
        sub(/[0-9]+/,"",$1)
            print "Line::"   l
            print "Field1::" a
            print "Field2::" $1
            print "Field3::" $2 FS $3
            print "Field4::" $4
            print "Field5::" $5
            print "Field6::" $6
            print "Field7::" $7
    }
    ' file
Line::1234567891XYZABC 13/08/09 00:00:00 67890 12345 67890 00
Field1::1234567891
Field2::XYZABC
Field3::13/08/09 00:00:00
Field4::67890
Field5::12345
Field6::67890
Field7::00

Post 1111 :wink:

Hi danmero

Thanks a lot for your response and worked for us files with a delimiter as space. But in another case we have to use the same logic to read a fixed length file wherein in the above case

e.g.

the field length is fixed such as
Field 1 is 5 as length
Field 2 is 5 as length
Field 3 is 10 as length and so on

i.e. the file should not be read with a delimiter and instead should read as per the length we specify as mentioned.

Thanks
Meva

Be welcome :wink:

That's another case :rolleyes: where you should use substr() function, something like.

# awk '{
            print "Line::"   $0
            print "Field1::" substr($0,1,5)
            print "Field2::" substr($0,6,5)
            print "Field3::" substr($0,11,8)
            print "Field4::" "etc...... "
    }
    ' file

Good Luck! :b:

Hi Danmero,

Thanks!!! It worked...