Hacker Public Radio

HPR2804: Awk Part 13: Fix-Width Field Processing


Listen Later

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
...more
View all episodesView all episodes
Download on the App Store

Hacker Public RadioBy Hacker Public Radio

  • 4.2
  • 4.2
  • 4.2
  • 4.2
  • 4.2

4.2

34 ratings


More shows like Hacker Public Radio

View all
The Infinite Monkey Cage by BBC Radio 4

The Infinite Monkey Cage

1,952 Listeners

Click Here by Recorded Future News

Click Here

418 Listeners

Hacker And The Fed by Chris Tarbell & Hector Monsegur

Hacker And The Fed

168 Listeners