| Newsgroups |
gmane.editors.sed.user |
| Message-ID |
<[email protected]> |
Ruud, you're right, you figured out the shortest solution, and it does
not require knowledge of the line length. Again using dashes instead of
blanks, my original simple-minded attempt does NOT work, \1 is greedy
and eats up the entire line, so \2 is always empty.
$ cat input.txt
9----
----9
--9--
-9-9-
--9-9
9-9--
-----
99999
$ sed -r 's/^(.*)(-*)$/\2\1/' input.txt
9----
----9
--9--
-9-9-
--9-9
9-9--
-----
99999
Your solution does work, because you stop \1 from being so greedy. [^-]
stops \1 from eating up the entire line, so \2 takes the trailing spaces
(dashes here), which get moved to the front, just as you said.
$ sed -r 's/^(.*[^-])(-*)$/\2\1/' input.txt
----9
----9
----9
--9-9
--9-9
--9-9
-----
99999
I did slightly modify your solution, adding needed anchor '$' to correct
a glitch processing line 5 in the test case above.
Thanks,
Daniel
On 11/15/2015 6:33 AM, 'Ruud H.G. van Tol' [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
>
> -- Ruud
>
>
> ------------------------------------
> Posted by: "Ruud H.G. van Tol"
> ------------------------------------
>