Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell command to reverse each line of file

Tags:

bash

shell

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?

like image 320
abcdef Avatar asked Jul 16 '15 07:07

abcdef


People also ask

How do you reverse a line in shell script?

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.

How do you reverse the each line of a file in Linux?

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.

How do I reverse the contents of 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.


1 Answers

rev will do the job:

rev file
4 3 2 1
6 5 4 3
2 3 7 8
like image 128
anubhava Avatar answered Dec 05 '22 01:12

anubhava