How to use the command 'tac' (with examples)
- Linux
- December 25, 2023
The ’tac’ command is used to display and concatenate files with lines in reversed order. It can be seen as the reverse of the ‘cat’ command.
Use case 1: Concatenate specific files in reversed order
Code:
tac path/to/file1 path/to/file2 ...
Motivation: The ’tac’ command is convenient when you want to combine the contents of multiple files into a single file, but in reverse order. This can be useful when you want to view the most recent changes in a log file or when you need to process a file in reverse chronological order.
Explanation: The ’tac’ command is followed by the paths of the files you want to concatenate. You can specify multiple file paths to concatenate them together.
Example output:
If ‘file1’ contains:
Line 1
Line 2
Line 3
And ‘file2’ contains:
Line X
Line Y
Line Z
The command tac file1 file2
will output:
Line X
Line Y
Line Z
Line 3
Line 2
Line 1
Use case 2: Display ‘stdin’ in reversed order
Code:
cat path/to/file | tac
Motivation: This use case is useful when you want to reverse the order of lines in a file, especially when you want to quickly view the contents of a file in reverse.
Explanation: In this use case, we are using the ‘cat’ command to output the contents of a file and then piping it to the ’tac’ command. ‘stdin’ refers to the standard input.
Example output:
If ‘path/to/file’ contains:
Line 1
Line 2
Line 3
The command cat path/to/file | tac
will output:
Line 3
Line 2
Line 1
Use case 3: Use a specific separator
Code:
tac --separator , path/to/file1 path/to/file2 ...
Motivation: This use case is helpful when you want to specify a custom separator between the lines of the files being concatenated.
Explanation: In this use case, the ‘–separator’ option is used to specify a specific character as the separator between lines. In the example code, ,
is used as the separator.
Example output:
If ‘file1’ contains:
Line 1
Line 2
Line 3
And ‘file2’ contains:
Line X
Line Y
Line Z
The command tac --separator , file1 file2
will output:
Line X,Line Y,Line Z,Line 3,Line 2,Line 1
Use case 4: Use a specific regex as a separator
Code:
tac --regex --separator [,;] path/to/file1 path/to/file2 ...
Motivation: This use case is useful when you want to use regular expressions as separators between lines.
Explanation: In this use case, the ‘–regex’ option is used to enable the usage of regular expressions as separators. The ‘–separator’ option is then followed by the regular expression to be used as the separator. In the example code, [,]
and ;
are used as the regular expressions.
Example output:
If ‘file1’ contains:
Line 1
Line 2
Line 3
And ‘file2’ contains:
Line X
Line Y
Line Z
The command tac --regex --separator [,;] file1 file2
will output:
Line X;Line Y;Line Z;Line 3,Line 2,Line 1
Use case 5: Use a separator before each file
Code:
tac --before path/to/file1 path/to/file2 ...
Motivation: This use case is helpful when you want to add a separator before each file when concatenating them.
Explanation: In this use case, the ‘–before’ option is used to add a separator before each file. This can be useful to distinguish the contents of individual files when concatenating them.
Example output:
If ‘file1’ contains:
Line 1
Line 2
Line 3
And ‘file2’ contains:
Line X
Line Y
Line Z
The command tac --before file1 file2
will output:
===> file1 <===
Line 1
Line 2
Line 3
===> file2 <===
Line X
Line Y
Line Z
Conclusion:
The ’tac’ command is a versatile tool for displaying and concatenating files in reverse order. It offers several options to customize the behavior according to your specific requirements. Whether you need to process log files, reverse the order of file contents, or add separators between lines, the ’tac’ command can be a helpful addition to your command-line toolkit.