Extract Fixed Columns from Pipelines with col1 to col9 and NF
You will finish with short, readable commands for selecting a numbered field from line-oriented input. The col1 through col9 commands select that field, while NF selects the last field. They are small shell wrappers around awk, installed here by byobu 6.11-0ubuntu1.1.
The route
Jump straight to the step you need, or tick off Done means at the end.
Allow about ten minutes. You need a shell and the byobu package. The examples only read input and write to standard output. They do not edit the input, change services or require elevated privileges.
1. Check which helper is installed
Confirm that the command is on your path and record 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 retain the simple interface described below.
Checkpoint: if command -v prints nothing, stop here and install byobu through your normal package-management process. Do not copy a script into /usr/local/bin just to make a pipeline work, because that can hide which implementation a future shell will run.
2. Select a field from whitespace-separated input
With no argument, the helper uses whitespace as the field separator. 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. This is useful for quick inspection of output such as mount, ps or a purpose-built command that emits stable whitespace-separated fields.
There is no special option for choosing the column. A command called through a different name gets a different field number, so check the spelling before debugging the input.
3. Select the last field with NF
NF is the alias for the awk concept meaning the number of fields on the current record. In this helper, it prints the last field on each input line:
$ printf '%s\n' 'alpha 10 ready' 'beta 20 waiting' | NF
ready
waiting
This is often safer than choosing a high fixed number when the number of leading fields can vary. It is still a positional operation: if the input line has a trailing value that is not the one you meant, NF will print that trailing 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 when the input is not whitespace
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 is handed to awk as its field-separator expression. It is not a quoted CSV parser and does not remove CSV quoting or interpret commas inside quoted fields. For simple delimiter-separated text, it is enough. For real CSV, use a parser that understands the format.
The argument is consumed by the helper; any later arguments are passed to awk as input files. That makes a file operand convenient:
$ col3 : /path/to/status.txt
ready
waiting
Replace /path/to/status.txt with a readable file. This command does not require sudo unless the file itself is restricted. If the file contains sensitive data, remember that the selected values are printed to your terminal or whatever process receives standard output.
5. Check missing fields before trusting the output
If a line has fewer fields than the helper requests, the installed command prints a blank line and still exits successfully. That is a normal consequence of asking awk for a field that is not present, not proof that the source contained an empty value:
$ printf '%s\n' 'one two' | col9 | od -An -t x1
0a
The byte 0a is a newline with no field content. Guard a pipeline when an empty selection would be dangerous:
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
This check is appropriate when the selected value controls a later command. For multi-line input, use 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. This 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 col3 when the compact command is clearer in a pipeline. Use explicit awk when the script also needs conditions, named variables, quoting-aware parsing or more than one selected field. The helper does not provide those features.
Do not use col1 to col9 as a generic file-editing tool. Redirection such as > output.txt creates or truncates the destination before the rest of the pipeline runs. If you need a saved result, choose a new destination and inspect it before replacing an existing file. The helper itself has no undo operation.
Done means
- The installed helper is provided by the expected
byobupackage. col1tocol9select numbered fields using whitespace by default.NFselects the last field, including when records have different field counts.- A delimiter is passed as the single optional separator argument, with CSV quoting limitations understood.
- Missing fields and output redirection are checked before the command is used in a consequential script.