banner

For a list of BASHing data 2 blog posts see the index page.    RSS


Filling down blanks in multiple fields

The title of this post might be a bit cryptic, so I'll explain it with an example. Here's the tab-separated table "filldowner":

fld1fld2fld3fld4fld5fld6
2oQ3K6Ef2I4HH17
W4o2iWU7JV58
qC5Ro0v0L7XE
0EN1NiJ6aQD4fS6
VA3x8VrL86Pvo5E
DY2W9cA6Jo1EBN5S0w
SB39SSzA3T5NXz9
1Dhz8YD41J3MjY9
O7rU1xz2P2eUnA5
p2DvT407AV1YRG42Xd

I want to replace the blanks in each field with the last non-blank value in that field, like this:

fld1fld2fld3fld4fld5fld6
2oQ3K6Ef2I4HH17
W4o2iWU7JV58H17
qC5Ro0v0LU7J7XEH17
0EN1NiJ6aU7JQD4fS6
VA3x8VrL8U7J6Pvo5E
DY2W9cA6Jo1EBN5S0w
SB3W9c9SSzA3T5NXz9
1DhW9cz8YD41J3MjY9
O7rW9cU1xz2P2eUnA5
p2DvT407AV1YRG42Xd

And I'd like to do the filling-down with a one-liner, and ignore the header (otherwise I'd be filling down with the field name if the first record had a blank field).

The most direct way to do this job is to use AWK and an AWK array:

awk -F"\t" 'NR>1 {for (i=1;i<=NF;i++) if ($i!="") {a[i]=$i} else {$i=a[i]}} 1' OFS="\t" filldowner

awk-direct

In everyday language the command reads In this tab-separated file, ignore the header line and work through the fields one-by-one in each record. If the data item in the field isn't blank, store the data item in an array "a" indexed with the field number. If the data item is blank, fill the blank with the last array value indexed with that field. Print all records with tab as field separator.

In case you're wondering, I generated the 6 x 10, tab-separated matrix of random 3-character data items in "filldowner" with the command pwgen 3 -N 60 | xargs -n 6 | tr " " "\t". I then manually deleted data items to create blanks, added a header line and HTML'ed the result as a table. pwgen is a marvellous utility from Theodore Ts'o and I use it a lot!


Next post:
2025-09-05   5, 7, 8, 9, 10, 12, 14, 15, 17. Any advance on 17?


Last update: 2025-08-29
The blog posts on this website are licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License