Shell script reverse lines of a file

‮al.www‬utturi.com
Shell script reverse lines of a file

To reverse the lines of a file in a shell script, you can use the tac command, which is available on most Unix-like systems. Here's an example of how you might use tac in a shell script:

#!/bin/bash

# Reverse the lines of a file
tac input.txt > output.txt

This script will read the input file, input.txt, reverse the lines of the file, and write the reversed lines to output.txt.

Alternatively, you can use the sed command to reverse the lines of a file. Here's an example of how you might use sed to do this:

#!/bin/bash

# Reverse the lines of a file
sed -n '1!G;h;$p' input.txt > output.txt

This script will read the input file, input.txt, and use the sed command to reverse the lines of the file. The reversed lines will be written to output.txt.

You can also use the rev command to reverse the lines of a file. Here's an example of how you might use rev to do this:

#!/bin/bash

# Reverse the lines of a file
rev input.txt > output.txt

This script will read the input file, input.txt, and use the rev command to reverse the lines of the file. The reversed lines will be written to output.txt.

Created Time:2017-10-30 14:27:18  Author:lautturi