Skip to content
Advertisement

Bash – Move last word in line to beginning of line

I need to move (in a text file) the last word on a line to the beginning of that line.

From This:

I Am Legend (2007)
RoboCop (1987)
Shrek (2001)

To This:

(2007) I Am Legend
(1987) RoboCop
(2001) Shrek

Advertisement

Answer

With GNU sed:

sed 's/(.*)((.*))/2 1/' file

Output:

(2007) I Am Legend 
(1987) RoboCop 
(2001) Shrek 

To edit file “in place”:

sed -i 's/(.*)((.*))/2 1/' file
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement