Reverse all words in current line

Posted by KasiyA on Ask Ubuntu See other posts from Ask Ubuntu or by KasiyA
Published on 2014-08-22T14:30:15Z Indexed on 2014/08/22 16:36 UTC
Read the original article Hit count: 324

I have a file and I want to reverse all word in it. Read line as long as (.) not seen, or seen (\n), if found first (.) in line then It is a word , so reverse this word and continue reading for next word in current line until end of file.

ex input file:

DCBA. HGFE.GI
MLK,PON.RQ
UTS. ZYXWV. 321

ex output file: (What I Want)

ABCD. EFGH.IG
KLM,NOP.QR
STU. VWXYZ. 123

With this sed script: sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//' in the entire line is reversed.

The wrong output produced by the command above:

IG.EFGH .ABCD    
QR.NOP,KLM    
123 .VWXYZ .STU 

How can I get my desired output? Thanks for your help

© Ask Ubuntu or respective owner

Related posts about command-line

Related posts about files