| Newsgroups |
gmane.editors.sed.user |
| Message-ID |
<[email protected]> |
On 2015-11-16 05:27, Daniel Goldman [email protected] [sed-users] wrote:
> On 11/15/2015 6:33 AM, 'Ruud H.G. van Tol' [sed-users] wrote:
>> 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, 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.
> [...]
> $ 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.
Good correction, thanks.
You appear to have missed that it had ' *',
which has 2 spaces, to keep it from doing busywork.
Some other ways to express the same:
[ ][ ]*
[ ]{1,}
--
Ruud
sed 's/^\(.*[^-]\)\(--*\)$/[\1][\2] -> [\2][\1]/' input-.txt
[9][----] -> [----][9]
----9
[--9][--] -> [--][--9]
[-9-9][-] -> [-][-9-9]
--9-9
[9-9][--] -> [--][9-9]
-----
99999