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

Take your Linux skills to the next level!

Command Line Kung Fu

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

Command Line Kung Fu is packed with dozens of tips and practical real-world examples. You won't find theoretical examples in this book. The examples demonstrate how to solve actual problems. The tactics are easy to find, too. Each chapter covers a specific topic and groups related tips and examples together.

$ Check price
14.9914.99Amazon 4.5 logo(27+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Inversion Therapy Table

Aching back from coding all day? Try Back Problems

Stretch out your back and relieve your back muscles with inversion therapy. This device counteracts the forces of gravity on the body by decompressing and elongating the spine. By using this product just ten minutes a day, you can be well on your way to improved circulation and posture while relieving muscle aches, back pain and stress.

$$ Check price
119.98119.98Amazon 4.5 logo(1,700+ reviews)

More Back Problems resources

Ad