The commands cat and tac display the contents of text files, but they have more to offer than you think. Dive in a bit deeper and learn some productive Linux command line tricks.
These are two simple little commands that are often dismissed as just that ̵
The cat Command
cat is used to examine the contents of text files and to merge parts of files into a larger file.
Earlier – at the time of dial-up modem – binaries were often split into several smaller files to simplify downloading. Instead of downloading a large file, you have withdrawn any smaller file. If a single file could not be downloaded correctly, it was simply retrieved again.
Of course, you then had to turn the collection of smaller files back into a working binary file. This process was called chaining. And here came
cat and that's where the name comes from.
Broadband and fiber optic connections have led to a reduction in the particular need for decay – similar to the squeaky dialing noise. So what's left for
? cat to do today? Pretty much, actually.
Viewing a Text File
cat the contents of a text file in a terminal window, use the following command.
Make sure the file is a text file. If you try to list the contents of a binary file in the terminal window, the results are unpredictable. The terminal session may be blocked or worse.
The contents of the poem1.txt file are displayed in the terminal window.
That's only half of the famous poem. Where is the rest of it? There is another file called poem2.txt here. We can cause
cat to list the contents of multiple files with one command. All we have to do is list the files in the command line in the given order.
cat poem1.txt poem2.txt
That looks better; We now have the whole poem.
Cat with less use windows too fast to read the first verses. We can redirect the output of
less and scroll through the text at our own pace.
cat poem1.txt poem2.txt | less
We can now scroll forwards and backwards through the text in a stream, even though it is stored in two separate text files.
Numbering lines in a file
We can number the lines in the file as they appear. For this we use the option
cat -n poem1.txt
The lines are numbered as they appear in the terminal window.
No blank lines number
We managed to have the lines numbered by
cat as well the empty lines between the verses are counted. Use the option
-b (number-nonblank) to number the lines of text but ignore the blank lines.
cat -b poem1.txt
Now the text lines are numbered and the blank lines are skipped.
Don & # 39; t Show Multiple Blank Lines
If a file contains sections of consecutive blank lines, we can ask
cat to ignore all but a blank line. Look at this file.
The next command causes
cat to display only one blank line from each group of blank lines. The option we need to accomplish this is the option
cat -s poem1.txt
 This does not affect the contents of the file. it just changes the way
cat displays the file.
If you want to know if whitespace exists Problems caused by spaces or tabs can be found out with the option
cat -T poem1.txt
The tabs are represented by the characters "^ I".
Viewing the End of the Line
How to Check for Trailing Spaces by Using the
-E (show-ends) use.
cat -E poem1.txt
The line endings are represented by the character "$".
It is not possible It makes sense to save a poem in two files, one half each. Let's put it together and create a new file with the entire poem.
cat poem1.txt poem2.txt> jabberwocky.txt
cat to get our new file check:
Our new file contains the content of the other two files.
Attaching text to an existing file
That's better, but in fact it's not the entire poem. The last verse is missing. The last verse in Jabberwocky is the same as the first verse.
If we have the first verse in a file, we can add it to the end of the file jabberwocky.txt and have the complete poem.
In this next command we have to use
>> not just
> . If we use a single
> we will overwrite jabberwocky.txt. We do not want that. We would like to append text to the bottom .
cat first_verse.txt >> jabberwocky.txt
 Let's check the contents of the jabberwocky.txt file:
And finally all parts of the poem are together
cat lets you redirect input from the keyboard to a file. Everything you enter will be redirected to the file until you press Ctrl + D. Note that we use a single
> because we want to create the file (or override if it exists).
Once we enter the command, we can begin typing. We press Ctrl + D when we're done. We can then check the contents of the new file with the following command:
That sounds like a distant turbine that Lewis Carroll probably turns in his grave at high speed.
The tac command
tac is similar to
cat but lists the contents of files in reverse order.
19659013] tac my_poem.txt
The file is listed in reverse order in the terminal window. In this case it has no effect on its literary advantages.
Using tac with stdin
tac without filenames causes the input to be processed through the keyboard. If you press Ctrl + D, the input phase stops, and tac is listed in reverse order, whatever you entered.
When Ctrl + D On a hit, the input is reversed and listed in the terminal window.
Use tac with log files
Apart from inferior parlor tricks Can
tac do something useful? Yes, it can. Many log files append their most recent entries at the end of the file. With
tac (and intuitively with
head ) we can place the last entry in the terminal window.
tac to reverse list the syslog file and direct it to the
head . If you instruct
head to print only the first line received (which is the last line in the file, thanks to
tac ), the most recent entry in the syslog file will be displayed.
tac / var / log / syslog | head -1
head prints the last entry from the syslog file and then stops.
head prints only one line – as we wished – but the line is so long that it circles twice. That's why the terminal window looks like three output lines.
Using tac with text records
The last trick
tac has the sleeve full, is a beauty.
tac edits text files by working them from bottom to top line by line. A line is a sequence of characters terminated by a newline character. But we can instruct
tac to work with other delimiters. In this way, we can treat blocks of data in the text file as records.
Let's say we have a log file from a program that we need to review or parse. Let's take a look at the format with
less log file.dat
As we can see, there is a repeating format for the file. There are sequences of three lines of hexadecimal values. Each set of three hexadecimal rows has a caption line beginning with "= SEQ" followed by a sequence of digits.
If we scroll down the bottom of the file, we can see that there are many of these records. The last one is numbered 865.
Let's assume that for some reason we have to work through this file in reverse order, record by record. The row order of the three hexadecimal rows in each record must be preserved.
We will point out that the last three lines in the file begin with the hexadecimal values 93, E7, and B8 in that order.
tac to reverse the file. It is a very long file, so we pass it in
tac logfile.dat | continue less
This reverses the file, but it is not the desired result. We want the file to be reversed, but the lines in each record must be in the original order.
We recorded earlier than the last three lines in the file, starting with the hexadecimal values 93, E7, and B8 in that order. The order of these lines was reversed. In addition, the "= SEQ" lines are now under three hexadecimal lines each.
tac to the rescue.
tac -b -r -s ^ = SEQ. + [0-9] + * $ logfile.dat | less
Let's resolve that.
-s (separator) informs
tac about what we want to use as separators between our records. It instructs
tac not to use the usual newline, but to use our delimiter instead.
-r (regex) instructs
tac to treat the delimiter string as a regular expression.
-b (before) causes
tac to list the delimiter before for each record instead of for it (this is the usual position of the standard separator ( the line break character).
^ = SEQ. + [0-9] + * $ is decrypted as follows:
^ stands for the beginning of the line, followed by
= SEQ. + [0-9] + * $ This indicates
tac after every occurrence of "= SEQ." At the beginning of the line followed by any digit string (specified by
[0-9]) and followed by any other character set (specified by
* $ ).
We derive the entire lot as usual in
Our e file is now displayed in reverse order, listing each "= SEQ" label line before the three lines of hexadecimal data. The three lines of hexadecimal values are in their original order in each record.
We can easily verify this. The first value of the first three hexadecimal lines (the last three lines before reversing the file) matches the values we recorded earlier: 93, E7, and B8, in that order.
This is quite a mistake trick for a single-line terminal window.
Everything has a purpose.
In the Linux world, even the seemingly simplest commands and utilities can have surprising and powerful features.
The design philosophy of simple utilities that perform well, and what easily work together with other utilities, has led to some weird little commands, such as
tac . At first glance, it seems a bit strange. But if you look below the surface, there is an unexpected power that you can use to your advantage.
Or, as another philosophy says, "Do not despise the serpent because it has no horns, because who should say that, should not it become a dragon? "