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?

Foam Seat Cushion

Aching back from coding all day? Try Back Problems

This foam seat cushion relieves lowerback pain, numbness and pressure sores by promoting healthy weight distribution, posture and spine alignment. Furthermore, it reduces pressure on the tailbone and hip bones while sitting. Perfect for sitting on the computer desk for long periods of time.

$ Check price
99.9599.95Amazon 4.5 logo(9,445+ reviews)

More Back Problems resources

Take your Linux skills to the next level!

Linux for Beginners

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

Linux for Beginners doesn't make any assumptions about your background or knowledge of Linux. You need no prior knowledge to benefit from this book. You will be guided step by step using a logical and systematic approach. As new concepts, commands, or jargon are encountered they are explained in plain language, making it easy for anyone to understand.

$ Check price
24.9924.99Amazon 4.5 logo(101+ reviews)

More Linux & UNIX resources

Ad