قالب وردپرس درنا توس
Home / Tips and Tricks / Using the Linux commands cat and tac

Using the Linux commands cat and tac



  Type of Terminal Window on a Linux Desktop
Fatmawati Achmad Zaenuri / Shutterstock.com

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 ̵

1; too simple to be of any use. However, if you know the various uses, you will find that they are able to make an appropriate contribution to lifting loads when working with files.

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

To list 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.

  cat poem1.txt 

  cat poem1.txt in a terminal window

The contents of the poem1.txt file are displayed in the terminal window.

 The content of poem1.txt in a 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 

  cat poem1.txt poem2.txt in a terminal window

That looks better; We now have the whole poem.

 Contents of poem1.txt and poem2.txt in a terminal window.

Cat with less use windows too fast to read the first verses. We can redirect the output of cat to less and scroll through the text at our own pace.

  cat poem1.txt poem2.txt | less 

  cat poem1.txt poem2.txt | less in a terminal window

We can now scroll forwards and backwards through the text in a stream, even though it is stored in two separate text files.

 Content of poem1.txt and poem2. txt in less in a terminal window

Numbering lines in a file

We can number the lines in the file as they appear. For this we use the option -n (number).

  cat -n poem1.txt 

  cat -n poem1.txt in a terminal window

The lines are numbered as they appear in the terminal window.

 Content of poem1.txt with numbered lines in a 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 

  cat -b poem1.txt in a terminal window

Now the text lines are numbered and the blank lines are skipped.

 numbered lines that skip blank lines in a terminal window.

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 contents of a file with consecutive blank lines in a terminal window

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 -s (squeeze-blank).

  cat -s poem1.txt 

  cat -b poem1.txt in a terminal window [19659004] This does not affect the contents of the file. it just changes the way cat displays the file.

 Multiple blank lines are ignored in a terminal window.

Show tabs

If you want to know if whitespace exists Problems caused by spaces or tabs can be found out with the option -T (show-tabs).

  cat -T poem1.txt 

  cat -T poem1.txt in a terminal widow

The tabs are represented by the characters "^ I".

 Contents of poem1.txt with Tabs in a Terminal Window

Viewing the End of the Line

How to Check for Trailing Spaces by Using the -E (show-ends) use.

  cat -E poem1.txt 

  cat -T poem1.txt in a terminal window

The line endings are represented by the character "$".

 Content of poem1.txt with end-of-line appearing in a terminal window.

Chaining files

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 poem1.txt poem2.txt> jabberwocky.txt in a terminal window

Let's use cat to get our new file check:

  cat jabberwocky.txt 

  cat jabberwocky.txt in a terminal window

Our new file contains the content of the other two files.

 Content of jabberwocky.tx in a terminal window

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 

  cat first_verse.txt >> jabberwocky.txt in a terminal window [19659004] Let's check the contents of the jabberwocky.txt file:

  cat jabberwocky.txt 

  cat jabberwocky.txt in a terminal window

And finally all parts of the poem are together

]  Content of jabberwocky.txt in a terminal window

Redirecting stdin

Using 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).

  cat> my_poem.txt 

  cat> my_poem.txt in a terminal window

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:

  cat my-poem.txt 

  Malignant Doggerel in a Terminal Window

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

  tac my_poem.txt in a terminal window

The file is listed in reverse order in the terminal window. In this case it has no effect on its literary advantages.

 my_poem.txt listed in reverse order in a terminal window

Using tac with stdin

Using 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.

  tac 

  tac works with stdin in a terminal window

When Ctrl + D On a hit, the input is reversed and listed in the terminal window.

 Issue of tac with stdin in a 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.

We use 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 

  tac / var / log / syslog | head -1 in a terminal window

head prints the last entry from the syslog file and then stops.

Note that 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.

 Last entry of syslog in a terminal window

Using tac with text records

The last trick tac has the sleeve full, is a beauty.

Normally, 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 .

  less log file.dat 

  less log file in a terminal window

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.

 At the top of the log file in a terminal window

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.

 End of the log file in a terminal window

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.

Let's use tac to reverse the file. It is a very long file, so we pass it in less .

  tac logfile.dat | continue less 

  tac logfile.dat | less in a terminal window

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.

 Log file reversed in less in a terminal window

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 

  tac -b -r -s ^ = SEQ. + [0-9] + * $ logfile.dat | less in a terminal window

Let's resolve that.

The option -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.

The option -r (regex) instructs tac to treat the delimiter string as a regular expression.

The option -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).

The string -s (delimiter) ^ = SEQ. + [0-9] + * $ is decrypted as follows:

The character ^ 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 less .

 reverse log file with correctly formed records

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? "




Source link