Awk with fixed length files

Hi Unix Champs,

I want to awk on a fixed length file.
Instead if the file was a delimited file, then I could have used -F and then could have easily done manipulation on the fields.
How do i do the same in case of fixed length file?

Thanks in Advance.

Regards.

Sorry! :stuck_out_tongue:

Whats your actual requirement?

Could you please post sample Input and Output?

Say the file format is like:

Record length: 1-30

Fields 1-10 > Emp ID
Fields 11-20 > Emp Name
Fields 21-30 > Contact No

I want 2 perform some awk operations.
Is there any option like -F is case of delimited file.

Still your requirement is not clear. I'll ask you to put here some sample input and output you require. But for fixed length situation you can try use substr function in awk, like substr($0,1,10) for emp ID and substr($0,11,10) for emp name and so on.

There are going to be a lot of manipulations only if couple of fields' values.

Now if the file was a delimited, i could have written something like

awk '$2=something && $3=somthingelse { some action }'

But now since the file is fixed length, a lot of processing processing time will be wasted just on substring and then check wheten to perform the acton. Any solution?

Post sample input and sample output, no one can suggest you until you clarify your requirements.

"Don't use fixed length files" pops quickly to mind. "Switch to tools with support for fixed length files" is another thought. perl is probably the most widespread language with fixed field support.

If you must use awk on a fixed field file, maybe you could insert delimiters first:

$ cat input
111111111122222222223333333333
aaaaaaaaaabbbbbbbbbbcccccccccc
qqqqqqqqqqggggggggggtttttttttt
$ sed 's/\(.\{10\}\)\(.\{10\}\)\(.\{10\}\)/\1-\2-\3/' < input
1111111111-2222222222-3333333333
aaaaaaaaaa-bbbbbbbbbb-cccccccccc
qqqqqqqqqq-gggggggggg-tttttttttt
$

Thnks 4 ur suggestion.
Iwil convert it into a csv.