Linux tac command

It works opposite of the ‘cat’ command. The ‘tac’ command is also called ‘cat’ backward. It helps display the content of the file in reverse order. It displays the last line of the file first and then it will print the second last line and so on. In this way, the first line will be printed at last.

Syntax:

tac <file name>

Example: tac count

codes@w3schools:~/Desktop$ cat count
one
two three
four five six
seven eight nine ten
codes@w3schools:~/Desktop$
codes@w3schools:~/Desktop$ tac count
seven eight nine ten
four five six
two three
one
codes@w3schools:~/Desktop$

As you can see in the above example that we have used both ‘cat’ and ‘tac’ command to display the file ‘count’ in order to show the difference between the two.

To Separate The Content:

This ‘tac –separator’ command is used to separate the content from the entered string or keyword from the rest content of the file.

Syntax:

tac <file name> --separator "<string>"

Example:

tac count --separator "two"
 
tac count --separator "five"
 
tac count --separator "one"
 
codes@w3schools:~/Desktop$ tac count --separater "two"
three
four five six
seven eight nine ten
one
twocodes@w3schools:~/Desktop$
codes@w3schools:~/Desktop$
codes@w3schools:~/Desktop$ tac count --separater "five"
 six
seven eight nine ten
one
two three
four fivecodes@w3schools:~/Desktop$
codes@w3schools:~/Desktop$
codes@w3schools:~/Desktop$ cat count --separater "one"
two three
four five six
seven eight nine ten
onecodes@w3schools:~/Desktop$
codes@w3schools:~/Desktop$

In the above example, first we are separating string “two” from the rest of the content of file ‘count’ by using ‘tac count –separator “two” ‘ command. Then, next we are separating the rest content from the string “five” and “one” respectively.

 

Please follow and like us:
Content Protection by DMCA.com