Extract Fixed Columns from Pipelines with col1 to col9 and NF
Grab the third field of a pipeline with col3 instead of typing awk '{print $3}' for the hundredth time. The col1 through col9 commands select that numbered field, and NF selects the last one. They are small shell wrappers around awk, installed here by byobu 6.11-0ubuntu1.1, and this takes about ten minutes.
The route
Jump straight to the step you need, or tick off Done means at the end.
- You need: a shell and the
byobupackage. - What it touches: nothing. The examples only read input and write to standard output. No edits, no services, no elevated privileges.
1. Check which helper is installed
Confirm the command is on your path and note the package version:
$ command -v col3
/usr/bin/col3
$ dpkg-query -W -f='${Package} ${Version}\n' byobu
byobu 6.11-0ubuntu1.1
Your package version can differ. The manual page installed with this version is dated 27 September 2010, and the helpers keep the simple interface described below.
Checkpoint
If command -v prints nothing, stop and install byobu through your normal package management. Do not copy a script into /usr/local/bin just to make a pipeline work, because that hides which implementation a future shell will run.
2. Select a field from whitespace-separated input
With no argument, the helper splits on whitespace. The command name supplies the field number:
$ printf '%s\n' 'alpha 10 ready' 'beta 20 waiting' | col2
10
20
$ printf '%s\n' 'alpha 10 ready' 'beta 20 waiting' | col3
ready
waiting
Use col1 for the first field, col2 for the second, and so on through col9. It suits quick looks at the output of mount, ps or any command that emits stable whitespace-separated fields.
Tip
There is no option for choosing the column. The name is the column. If you get the wrong field, check you typed col3 and not col2 before you blame the input.
3. Select the last field with NF
NF is the awk term for the number of fields on the current record. Here it is a command that prints the last field of each input line:
$ printf '%s\n' 'alpha 10 ready' 'beta 20 waiting' | NF
ready
waiting
This is often safer than a high fixed number when the count of leading fields varies. It is still positional: if a line ends with a value you did not mean, NF prints that value.
Checkpoint
Test the exact shape of the real command output with a small sample before putting the helper into an alert, report or script.
4. Pass a delimiter for non-whitespace input
Give one optional argument to set the awk field separator. For comma-separated text, pass a comma:
$ printf '%s\n' 'alpha,10,ready' 'beta,20,waiting' | col3 ,
ready
waiting
$ printf '%s\n' 'alpha,10,ready' 'beta,20,waiting' | NF ,
ready
waiting
The separator goes to awk as its field-separator expression. It is not a CSV parser: it does not strip quoting or understand commas inside quoted fields. For simple delimited text that is enough. For real CSV, use a tool that understands the format.
The delimiter is consumed by the helper, and any later arguments go to awk as input files, so a file operand works neatly:
$ col3 : /path/to/status.txt
ready
waiting
Replace /path/to/status.txt with a readable file. No sudo is needed unless the file itself is restricted.
Warning
If the file holds sensitive data, the selected values are printed to your terminal or to whatever process receives standard output.
5. Check for missing fields
If a line has fewer fields than requested, the command prints a blank line and still exits successfully. That is just awk being asked for a field that is not there. It is not proof the source held an empty value:
$ printf '%s\n' 'one two' | col9 | od -An -t x1
0a
The byte 0a is a newline with no field content. When an empty selection would be dangerous, guard the pipeline:
value=$(printf '%s\n' 'alpha 10 ready' | col3)
if [ -n "$value" ]; then
printf 'selected: %s\n' "$value"
else
printf '%s\n' 'expected field was missing' &2
exit 1
fi
Use this check when the selected value controls a later command. For multi-line input, add a more explicit validation step if a blank line is not the only failure that matters.
6. Keep the field boundary visible
Before saving a command in a script, compare the helper with the equivalent awk expression. It makes the assumption reviewable:
$ printf '%s\n' 'alpha 10 ready' 'beta 20 waiting' | col3
ready
waiting
$ printf '%s\n' 'alpha 10 ready' 'beta 20 waiting' | awk '{print $3}'
ready
waiting
- Use
col3when the compact form reads better in a pipeline. - Use explicit
awkwhen you also need conditions, named variables, quoting-aware parsing or more than one selected field. The helper provides none of those.
Warning
Do not treat col1 to col9 as a file-editing tool. Redirection such as > output.txt creates or truncates the destination before the rest of the pipeline runs, and the helper has no undo. If you need a saved result, choose a new destination and inspect it before replacing anything.
Done means
- Package confirmed. The helper comes from the
byobupackage you expected. - Fields selected.
col1tocol9pick numbered fields, splitting on whitespace by default. - Last field handled.
NFpicks the last field, even when records have different field counts. - Delimiter understood. It is the single optional separator argument, and you know it is not a CSV parser.
- Risks checked. Missing fields and output redirection are dealt with before the command goes into a consequential script.