- Posted on
- • Advanced
Manipulating output with column and nl
- Author
-
-
- User
- Linux Bash
- Posts by this author
- Posts by this author
-
Enhancing Terminal Outputs with column
and nl
in Linux
When working with text data or terminal outputs in Linux, formatting the data for better readability and analysis can often become necessary. Two powerful yet underutilized utilities that assist with this task are column
and nl
. These tools help in structuring the outputs effectively, making them easier to read and understand. This article explores how you can manipulate output using these tools and explains how to install them using different package managers like apt
, dnf
, and zypper
.
Understanding column
and nl
column
is a utility that formats its input into multiple columns. Rows are filled before columns; you simply pipe output into column
and it presents it in a nicely formatted table format. On the other hand, nl
adds line numbers to its input, which is extremely useful for referencing or programming scenarios.
Installing column
and nl
Most GNU/Linux distributions include column
and nl
with the default installation as part of the util-linux
package and coreutils
respectively. However, if for some reason they are not present, they can easily be installed.
Debian/Ubuntu systems (using
apt
):sudo apt update sudo apt install bsdmainutils coreutils
Fedora systems (using
dnf
):sudo dnf check-update sudo dnf install util-linux coreutils
openSUSE systems (using
zypper
):sudo zypper refresh sudo zypper install util-linux coreutils
Using column
to Format Output
The column
tool is straightforward to use. For example, say you have a file named data.txt
containing several rows of data separated by spaces:
name age location
Alice 30 New_York
Bob 25 Los_Angeles
Clara 22 Chicago
You can pipe this content directly into column
like so:
cat data.txt | column -t
The -t
option determines the number of columns based on the input file’s content. Here’s what the output would look like:
name age location
Alice 30 New_York
Bob 25 Los_Angeles
Clara 22 Chicago
Numbering Lines with nl
The nl
command adds line numbers to each line. Running the following command will show numbering beside each line of your file:
nl data.txt
This outputs:
1 name age location
2 Alice 30 New_York
3 Bob 25 Los_Angeles
4 Clara 22 Chicago
Combining column
and nl
for Enhanced Output
Combining these tools can give you structured and numbered output, which can be extremely helpful for presenting data or debugging scripts.
cat data.txt | column -t | nl
This command will align all columns neatly and add a line number at the beginning of each line, enhancing both the readability and utility of the output.
Conclusion
The column
and nl
commands are simple yet powerful tools for formatting the output of text files or scripts in Linux. They are especially useful when you need to present data in a clearer format or when debugging scripts that require precise line references. By mastering these tools, Linux users can significantly improve their command-line efficiency and data handling capabilities.