That's an excellent question! The cat command, while seemingly simple, has a few other useful options that can make your life easier when working with text files.
Here are a couple of other commonly used cat options, along with what they do:
-
-b(or--number-nonblank): This option is similar to-n, but it specifically numbers only non-blank lines. Blank lines are not numbered.- When it's useful: If you have a file with many empty lines and only want to count lines that contain actual content.
- Example:
Let's saymy_file.txtcontains:Line 1 Line 3cat -n my_file.txtwould output:1 Line 1 2 3 Line 3cat -b my_file.txtwould output:
Notice how the blank line is not numbered with1 Line 1 2 Line 3-b.
-
-s(or--squeeze-blank): This option squeezes multiple adjacent blank lines into a single blank line.- When it's useful: If you're viewing a file that has excessive blank lines and you want to clean up the output for better readability.
- Example:
Let's sayanother_file.txtcontains:Content A Content Bcat another_file.txtwould output it as is.
cat -s another_file.txtwould output:
The two blank lines are squeezed into one.Content A Content B
-
-E(or--show-ends): This option displays a$character at the end of each line.- When it's useful: Very handy for debugging script files to see if lines inadvertently end with spaces, or to visually confirm where each line truly ends (especially in files that might have been edited on different operating systems that handle line endings differently).
- Example:
cat -E /tmp/hellowould output:Hi,$ I am Labby!$
You can also combine these options! For example, cat -ns /path/to/file would number non-blank lines and squeeze multiple blank lines.
These options make cat a surprisingly versatile tool for inspecting text files. Do any of these pique your interest, or would you like to explore how to use them?