hpr2804 :: Awk Part 13: Fix-Width Field Processing
In this episode, I discuss how to deal with fix-width field text files using Awk
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