How to Reverse Order of Lines in a File in Linux


As a Linux user, it is common to come across situations where you need to reverse order of lines in a file. This could be necessary when you want to read a file in a reverse order or when you want to perform certain operations that require content of a file to be in reverse order. In this article, we will explore how to reverse order of lines in a file in Linux.

Introduction to Linux Commands for Reversing Order of Lines in a File

Linux provides several commands that can be used to reverse order of lines in a file. These commands are designed to manipulate text and provide powerful options to achieve various tasks. In this article, we will focus on three commonly used commands: tac, sed, and awk.

Using tac Command to Reverse Order of Lines in a File

The tac command is a Linux command that is used to reverse order of lines in a file. tac command reads a file from end and prints each line in reverse order. Here is an example of how to use tac command to reverse order of lines in a file −

tac file.txt > reversed_file.txt

In this example, tac command is used to reverse order of lines in "file.txt" file, and output is redirected to a new file called "reversed_file.txt". This will create a new file with same content as original file but with order of lines reversed.

Using sed Command to Reverse Order of Lines in a File

The sed command is another Linux command that can be used to reverse order of lines in a file. sed command is a stream editor that is used to perform basic text transformations on an input stream. Here is an example of how to use sed command to reverse order of lines in a file −

sed '1!G;h;$!d' file.txt > reversed_file.txt

In this example, sed command is used to reverse order of lines in "file.txt" file, and output is redirected to a new file called "reversed_file.txt". sed command uses a regular expression to reverse order of lines in file.

Using awk Command to Reverse Order of Lines in a File

The awk command is a Linux command that is used to manipulate text files. awk command is a versatile command that can be used to perform various text processing tasks. Here is an example of how to use awk command to reverse order of lines in a file −

awk '{a[i++]=$0} END {for (j=i-1; j>=0;) print a[j--] }' file.txt > reversed_file.txt

In this example, awk command is used to reverse order of lines in "file.txt" file, and output is redirected to a new file called "reversed_file.txt". awk command uses an array to store lines of file and then prints lines in reverse order.

There are a few more advanced options that can be added to basic commands we have discussed above. These options provide further customization and control over how lines are reversed in file. Let's take a look at some of these options −

Using "-r" Option with tac Command

The tac command has an option called "-r" which can be used to reverse characters instead of lines. This option is useful when you want to reverse order of characters in each line. Here is an example −

tac -r file.txt > reversed_file.txt

In this example, "-r" option is added to tac command to reverse order of characters in each line of "file.txt" file. output is then redirected to a new file called "reversed_file.txt".

Using "-n" Option with sed Command

The sed command has an option called "-n" which can be used to suppress automatic printing of pattern space. This option is useful when you want to specify which lines to print. Here is an example −

sed -n '1!G;h;$p' file.txt > reversed_file.txt

In this example, "-n" option is added to sed command to suppress automatic printing of pattern space. "$p" command is then used to print last line, which reverses order of lines in file. output is then redirected to a new file called "reversed_file.txt".

Using "-F" and "-v" Options with awk Command

The awk command has two options "-F" and "-v" which can be used to set field separator and variables respectively. These options are useful when you want to specify how lines should be separated and manipulated. Here is an example −

awk -v RS='\r
' -v FS=',' '{a[i++]=$0} END {for (j=i-1; j>=0;) print a[j--] }' file.txt > reversed_file.txt

In this example, "-v RS='\r
'" option is added to set record separator to Windows-style line break. "-v FS=','" option is added to set field separator to a comma. rest of command is same as basic awk command we discussed earlier.

Conclusion and Summary

In conclusion, reversing order of lines in a file is a common task in Linux, and it can be achieved using several commands, including tac, sed, and awk commands. tac command is simplest and most straightforward command to use, while sed and awk commands are more powerful and provide more options and customization. choice of which command to use depends on your specific requirements and complexity of text processing task at hand.

In addition to basic commands discussed in this article, there are other Linux commands that can be used to reverse order of lines in a file, including rev command and Perl command. These commands provide additional options and flexibility for manipulating text files.

In summary, reversing order of lines in a file is a straightforward task in Linux, and there are several commands available to achieve this. tac, sed, and awk commands provide basic to advanced options for text processing, and choice of which command to use depends on your specific requirements. With a little practice and experimentation, you can master these commands and become proficient in manipulating text files in Linux.

Updated on: 24-Mar-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements