7.14. Odds and Ends
Some data (e.g., that read in from tape or from a spreadsheet) may not have obvious field separators but may instead have fixed-width columns. To preprocess this type of data, the substr function is useful.
7.14.1. Fixed Fields
In the following example, the fields are of a fixed width, but are not separated by a field separator. The substr function is used to create fields.
Example 7.81.
% cat fixed 031291ax5633(408)987–0124 021589bg2435(415)866–1345 122490de1237(916)933–1234 010187ax3458(408)264–2546 092491bd9923(415)134–8900 112990bg4567(803)234–1456 070489qr3455(415)899–1426 % nawk '{printf substr($0,1,6)" ";printf substr($0,7,6)" ";\ print substr($0,13,length)}' fixed 031291 ax5633 (408)987–0124 021589 bg2435 (415)866–1345 ... |
Get UNIX® Shells by Example, Third Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.