Hacker Public Radio

Your ideas, projects, opinions - podcasted.

New episodes Monday through Friday.

HPR2804: Awk Part 13: Fix-Width Field Processing

Hosted by Mr. Young on 2019-05-02 00:00:00
Download or Listen

Basic usage

Use the FIELDWIDTHS = "n1 n2 n3 ..." annotation in the BEGIN section of an awk command to specify the widths of the fields.

For instance, the following file has widths of 20, 10, and 12 characters.

NAME                STATE     TELEPHONE
John Smith          WA        418-311-4111
Mary Hartford       CA        319-219-4341
Evan Nolan          IL        219-532-5301
Boris Ratinski      NC        201-553-5555

Below is an example of processing such a file:

BEGIN  { FIELDWIDTHS = "20 10 12" }
NR > 1 {
    name = $1
    state = $2
    phone = $3
    sub(/ +$/, "", name)
    sub(/ +$/, "", state)
    sub(/ +$/, "", phone)
    printf("%s lives in %s. The phone number is %s.\n", name, state, phone)

Then you can run the command:

awk -f process_fixed_width.awk fixed_width.txt

HPR Comments

Mastodon Comments

More Information...

Copyright Information

Unless otherwise stated, our shows are released under a Creative Commons Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) license.

The HPR Website Design is released to the Public Domain.