tac display the contents of text files, but there is more to it than meets the eye. Dive a little deeper and learn some useful Linux command line tricks.
These are two simple little commands, often ignored as too simple to actually use. But once you know how they can be used in different ways, you will realize that they are quite capable of doing their hard work when it comes to working with files.
cat used to check the contents of text files and merge parts of files into one big file.
Back in the dial-up era, binaries were often split into several smaller files to make downloading easier. Instead of uploading one large file, you returned each smaller file. If any file failed to download correctly, you will simply get that file again.
Of course, then you needed a way to restore a collection of small files back into a single working binary. This process has been called unification. And that’s where it came from
cat and where he got his name from.
Broadband and fiber optic connections have made this special need disappear — just like the sounds of screeching dialing — so what’s left to do
cat today? Quite a lot actually.
Text file display
cat list the contents of a text file into a terminal window, use the following command.
Make sure the file is a text file. If you try to display the contents of a binary file in a terminal window, the results will be unpredictable. You could end up with a locked terminal session or worse.
The contents of the poem1.txt file are displayed in the terminal window.
This is only half of the famous poem. Where is the rest? There is another file here called poem2.txt. We can do it
cat list the contents of multiple files with a single command. All we have to do is list the files in order on the command line.
кот poem1.txt poem2.txt
It looks better; we have a whole poem now.
Use of a cat at a lower cost
The whole poem is there, but it flew past the window too quickly to read the first few verses. We can redirect output from
less and scroll down the text at your own pace.
кошка poem1.txt poem2.txt | Меньше
Now we can move forward and backward through the text in one stream, even if it is contained in two separate text files.
Line numbering in a file
We can specify the line number in the file as it is displayed. For this we use the option
cat -n poem1.txt
The lines are numbered as they appear in the terminal window.
Do not number empty lines
We managed to number the lines with
cat but empty lines between verses are also counted. To have text line numbering but ignore empty lines, use the option
cat -b poem1.txt
Now text lines are numbered and blank lines are skipped.
Don’t show multiple blank lines
If the file has sections of consecutive empty lines, we can ask
cat ignore all but one empty line. Look at this file.
The following command will force
cat display only one empty line from each group of empty lines. The option we need for this is the option
cat -s poem1.txt
This does not affect the contents of the file in any way; it only changes the way
cat displays the file.
If you want to know if spaces are caused by spaces or tabs, you can find out using the option
cat -T poem1.txt
Tabs are represented by the symbols «^I».
Line end display
You can check for spaces using the option
кот -E poem1.txt
Line ends are represented by the «$» symbol.
It does not make sense to save the poem in two files, one half in each. Let’s merge them together and create a new file with the entire poem.
cat poem1.txt poem2.txt> jabberwocky.txt
Our new file contains the contents of two other files.
Adding text to an existing file
It’s better, but it’s not really the whole 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 the file, we can add it to the end of the jabberwocky.txt file and we have the complete verse.
In this next command we have to use
>> not just
> . If we use one
> we overwrite jabberwocky.txt. We don’t want to do this. We want add text to the end.
cat first_verse.txt >> jabberwocky.txt
And finally, all parts of the poem together.
Redirecting standard input
You can redirect keyboard input to a file using
cat . Whatever you type is redirected to a file until you press Ctrl+D. Please note that we are using single
> because we want to create the file (or overwrite it if it exists).
This sound is like a distant turbine, probably Lewis Carroll spinning at high speed in the grave.
tac look like
cat but lists the contents of the files in reverse order.
Let’s see what:
And the file is displayed in the terminal window in reverse order. In this case, it does not affect its literary merit.
Using TAC with STDIN
tac without a filename will cause it to work with keyboard input. Pressing Ctrl + D will stop the input phase and tac will reverse everything you typed.
Pressing Ctrl + D reverses the input and displays it in the terminal window.
Using TAC with Log Files
Apart from low-quality tricks, can anything be done useful? Yes, it can. Many log files add their newest entries at the bottom of the file. Using
tac (and, illogically,
head ), we can pop the last entry into the terminal window.
tac to output the log file in reverse order and
head him in
head . talking
head print only the first line received (thanks to
tac is the last line in the file), we see the last entry in the log file.
tac / var / log / syslog | голова -1
head prints the last entry from the system log file and then exits.
head prints only one line — as we asked — but the line is so long that it wraps around twice. That’s why it looks like three lines of output in the terminal window.
Using TAC with text entries
The last trick that has sleeves is beauty.
tac works with text files, passing through them line by line, from bottom to top. A string is a sequence of characters ending with a newline character. But we can say
tac to work with other delimiters. This allows us to treat «chunks» of data in a text file as data records.
Let’s say we have a log file of some program that we need to view or analyze. Let’s look at its format with
As we can see, there is a repeating format in the file. There are sequences of three strings of hexadecimal values. Each set of three hexadecimal strings has a label string that starts with «=SEQ» followed by a sequence of digits.
If we scroll to the end of the file, we will see that there are many of these entries. Final number 865.
Let’s assume that for some reason we need to work through this file in reverse order, data entry by data entry. The line order of the three hexadecimal strings in each data entry must be preserved.
Note that the last three lines in the file start with the hexadecimal values 93, E7, and B8, in that order.