Site Map - skip to main content

Hacker Public Radio

Your ideas, projects, opinions - podcasted.

New episodes every weekday Monday through Friday.
This page was generated by The HPR Robot at


hpr2804 :: Awk Part 13: Fix-Width Field Processing

In this episode, I discuss how to deal with fix-width field text files using Awk

<< First, < Previous, , Latest >>

Thumbnail of Mr. Young
Hosted by Mr. Young on Thursday, 2019-05-02 is flagged as Explicit and is released under a CC-BY-SA license.
bash, linux, cli, command-line, awk. (Be the first).
The show is available on the Internet Archive at: https://archive.org/details/hpr2804

Listen in ogg, spx, or mp3 format. Play now:

Duration: 00:06:21

Learning Awk.

Episodes about using Awk, the text manipulation language. It comes in various forms called awk, nawk, mawk and gawk, but the standard version on Linux is GNU Awk (gawk). It's a programming language optimised for the manipulation of delimited text.

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

Comments

Subscribe to the comments RSS feed.

Leave Comment

Note to Verbose Commenters
If you can't fit everything you want to say in the comment below then you really should record a response show instead.

Note to Spammers
All comments are moderated. All links are checked by humans. We strip out all html. Feel free to record a show about yourself, or your industry, or any other topic we may find interesting. We also check shows for spam :).

Provide feedback
Your Name/Handle:
Title:
Comment:
Anti Spam Question: What does the letter P in HPR stand for?
Are you a spammer?
Who is the host of this show?
What does HPR mean to you?