I have a file test.txt with the following text
1 2 3 4
3 4 5 6
8 7 3 2
I want to save it as
4 3 2 1
6 5 4 3
2 3 7 8
Is there any shell command which does that?
rev command in Linux is used to reverse the lines characterwise. This utility basically reverses the order of the characters in each line by copying the specified files to the standard output. If no files are specified, then the standard input will read.
Its name comes from cat spelled backward, and tac functions as a reverse cat. Both commands belong to the coreutils package, which comes preinstalled in almost all Linux distributions. As we can see, tac reversed the output. tac is the most straight forward and efficient way of reversing a file.
Once the file is processed, the array is printed in the reverse order using the reverse function. In this, we keep popping the array element till the array has some element in it. By pushing and popping, it becomes like a stack,LIFO, due to which the file gets printed in the reverse order automatically.
rev
will do the job:
rev file
4 3 2 1
6 5 4 3
2 3 7 8
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With