find length of a fixed width file wtih a little twist

Issue

Hi Wonderful People/My Gurus and all kind-hearted people.

I’ve a fixed width file and currently i’m trying to find the length of those rows that contain x bytes. I tried couple of awk commands but, it is not giving me the result that i wanted. My fixed width contains 208bytes, but there are few rows that don’t contain 208 bytes. I”m trying to discover those records that doesn’t have 208bytes.

this cmd gave me the file length

awk '{print length;exit}' file.text

here i tried to print rows that contain 101 bytes, but it didn’t work.

awk '{print length==101}' file.text

Any help/insights here would be highly helpful

Solution

With awk:

awk 'length() < 208' file

Well, length() gives you the number of characters, not bytes. This number can differ in unicode context. You can use the LANG environment variable to force awk to use bytes:

LANG=C awk 'length() < 208' file

Answered By – hek2mgl

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published