Re: right justify each line?

"Paolo Bonzini [email protected] [sed-users]" <[email protected]>
Newsgroups gmane.editors.sed.user
Message-ID <[email protected]>

On 15/11/2015 15:33, 'Ruud H.G. van Tol' [email protected] [sed-users]
wrote:
> 
>  > $ cat input.x
>  > 99---
>  > ---99
>  > -9---
>  > -9-9-
>  > 99999
> 
> As was suggested before, if all lines have the right number of trailing 
> spaces, then just move those to the front.
> 
> sed 's~^\(.*[^ ]\)\(  *\)~\2\1~' input.txt

Here's another one that's about 3.5 times faster:

sed -n 's/ *$/\n&/;h;s/.*\n//;G;s/\n//;P' input.txt

Explanation:

    s/ *$/\n&/            # pattern space: 99\n---
    h                     # hold space:    99\n---
    s/.*\n//              # pattern space: ---
    G                     # pattern space: ---\n99\n---
    s/\n//                # pattern space: ---99\n---
    P                     # print          ---99

I timed the scripts on about 18000 120-character lines (a random source
code file I had around, padded to 120 characters and repeated four
times), and the results were around 110 ms for my script, 410 ms for Ruud's.

Paolo
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.