01. Cat Revisited cat

In a previous tutorial, we had a brief look at the cat command, and how it may be used to view or create files. Here, we'll look at it in a text-processing context.

Displaying non-printing characters

In any regular text file, there are a few "hidden" characters that are not printed. These non-printing characters are translated as part of the formatting. For example, the tab character has its own special character symbol, and is translated to a tab space when the file is opened in editors.

We can display non-printing characters with the following options:

-e
Display a $ at end of line.
-t
Display tab characters as ^I.
-v
Display control characters.

Additionally, we can show all non-printing characters with the -A shortcut.

$ cat sample.xml
# Regular format <?xml version="1.0" encoding="UTF-8"?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Please do not forget me this weekend!</body> </note>
$ cat -vet sample.xml
# Display hidden characters <?xml version="1.0" encoding="UTF-8"?>$ <note>$ ^I<to>Tove</to>$ ^I<from>Jani</from>$ ^I<heading>Reminder</heading>$ ^I<body>Please do not forget me this weekend!</body>$ </note>$

As you can see, this command outputs a ^I per each tab, and a $ per end of line.

unix2dos, dos2unix

So why do non-printing characters matter? In the UNIX world, there is a single line feed at the end of each line. In DOS format, however, there is a line feed and a carriage return. This portability issue can cause many bugs. To convert from one to the other, use the unix2dos or dos2unix command.

Viewing line numbers

To output line number, use the cat command with the -n option.

$ cat -n sample.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <note> 3 <to>Tove</to> 4 <from>Jani</from> 5 <heading>Reminder</heading> 6 <body>Please do not forget me this weekend!</body> 7 </note>

Suppressing blank lines

If you have a file with multiple blank lines, you can compress the viewing mode so that multiple blank lines appear only once. Use the -s option.

$ cat exampleWithBlankLines.txt
 
Dear user, Thank you for using me, the Linux Command Line instead of a boring old GUI! This really means a lot to me. I will make sure to stay efficient and easy to use - just as long as you promise to practice on my every day and night. For today's practice, let's see if you can get rid of all this unnecessary white space? Simply compress it with the -s option on the cat command! Best, CLI
$ cat -s exampleWithBlankLines.txt
# Suppress extra lines Dear user, Thank you for using me, the Linux Command Line instead of a boring old GUI! This really means a lot to me. I will make sure to stay efficient and easy to use - just as long as you promise to practice on my every day and night. For today's practice, let's see if you can get rid of all this unnecessary white space? Simply compress it with the -s option on the cat command! Best, CLI

Aching back from coding all day?

Acupressure Mat & Pillow

Aching back from coding all day? Try Back Problems

Relieve your stress, back, neck and sciatic pain through 1,782 acupuncture points for immediate neck pain relief. Made for lower, upper and mid chronic back pain treatment, and improves circulation, sleep, digestion and quality of life.

$$ Check price
144.87144.87Amazon 4.5 logo(1,890+ reviews)

More Back Problems resources

Take your Linux skills to the next level!

System Admin Handbook

Take your Linux skills to the next level! Try Linux & UNIX

This book approaches system administration in a practical way and is an invaluable reference for both new administrators and experienced professionals. It details best practices for every facet of system administration, including storage management, network design and administration, email, web hosting, scripting, and much more.

$ Check price
74.9974.99Amazon 4.5 logo(142+ reviews)

More Linux & UNIX resources

Ad