banner

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


How to keep an eye on field numbers

Both AWK and cut use numbers rather than names when operating on fields. For example, printing the second field in "file" could be done with:

awk '{print $2}' file
cut -f2 file

To relate field numbers to field names I usually rely on my fields function. All of my working files are tab-separated, so I use tr to convert the field-separating tabs in the header line to newlines before numbering the lines with pr.

fields() { head -n 1 "$1" | tr '\t' '\n' | pr -t -2 -n; }

For example:

fieldnos1

During a long data-auditing session with a file I might launch "fields" a dozen times, or repeatedly scroll back in the terminal to see the output from an earlier "fields" invocation. This can be tedious, so I coded a way to put a list of numbered fields outside the terminal, like a Post-it® note on my desktop. I pipe the numbered list to a suitably placed YAD dialog and run YAD as a background process. Putting the dialog in the background returns me to a prompt in the terminal. When I'm finished with the list of fields I return the YAD process to the foreground with the fg command and kill the process with Ctrl+c.

fieldlist() { head -1 "$1" | tr '\t' '\n' | nl | yad --geometry=350x800+1450+100 --text-info --no-focus & }

Here's the function "fieldlist" working on the file "mosc" after I'm back at the prompt:

fieldnos2

fieldlist() { head -1 "$1" | tr '\t' '\n' | nl | yad --geometry=350x800+1450+100 --text-info --no-focus & }
 
The command begins the same way that "fields" does (see above) but the field numbering is done with nl.
 
YAD puts the list in a dialog box 350 pixels wide and 800 high and places the box (with "+1450+100") just to the right of my terminal window (see screenshot above). The dialog type is "text-info" and "no-focus" means the workspace focus remains on the terminal and doesn't jump to YAD.
 
The final "&" puts the YAD dialog process in the terminal's background. Although the process is in the background and the YAD dialog is slightly grayed out (because its window is not in focus), scrolling down and up through a really long list of fields still works.


Last update: 2020-11-04
The blog posts on this website are licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License