in reply to Re: One-liner to append text at fixed column
in thread One-liner to append text at fixed column

If it comes to readability I prefer my last sprintf solution with the twist of changing if to and .

perl -lpe " /(match1|match2)/ and $_ = sprintf qq{%-68s  #$1}, $_ "

Normally I don't like short-circuit expressions, but in this case it really helps clarifying what's happening.

Demo: (win quoting)

>perl -E"say '.' x rand(80) for 1..10" |perl -lpe " /(.)/ and $_ = spr +intf qq{%-68s #$1},$_ " ...................................................................... +...... #. ...................... +#. ..................... +#. ...................................................................... +.... #. ......................................................... +#. ...................................... +#. ......................... +#. ........................................................ +#. ...................................................................... +... #. ......................... +#.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Replies are listed 'Best First'.
Re^3: One-liner to append text at fixed column
by Veltro (Hermit) on Nov 16, 2018 at 15:31 UTC

    Ok, I think I did not understand the original problem. I thought you were trying to 'align' all the comments from a text like this:

    perl -E"say '.' x rand(80) . '# test comment' for 1..3" ......................# test comment .................................# test comment ...................................................................... +....# test comment

    So eventually I came up with this which I think is too complex.

    perl -E"say '.' x rand(80) . '# test comment' for 1..10" |perl -lpe " +/\s*#(.*)$|$/ && ( substr( $_, $-[0], length($_) ) = qq{ }x(68 - $-[ +0] + 2) . qq{ #$1} ) ............................................... + # test comment .......................................................... + # test comment ....................................... + # test comment ............................................................ + # test comment ...................................................................... +.... # test comment ...... + # test comment ........................................ + # test comment ............................... + # test comment ................................................... + # test comment ................................... + # test comment

    Maybe it can be rewritten with a printf but I will leave it with this.

      > I thought you were trying to align all the comments from a text like this:

      Oh in this case I'd use the same "match-and-printf" construct, it's extremely flexible.

      D:\tmp>perl -E"say '.' x rand(70) . '# test comment' for 1..3" ....................# test comment ...................................# test comment .......................# test comment D:\tmp>perl -E"say '.' x rand(70) . '# test comment' for 1..20" |perl +-lpe " /(.*?)(#.*)/ and $_ = sprintf qq{%-68s %s},$1,$2" ..................... +# test comment .............................................................. +# test comment ..................................................................... + # test comment .................................... +# test comment .......................................................... +# test comment ............................................... +# test comment ............................................................ +# test comment ............. +# test comment ........................... +# test comment ............... +# test comment .............................. +# test comment .................................... +# test comment ................................................... +# test comment ................................. +# test comment ........................................................... +# test comment .......... +# test comment ....................................................... +# test comment ................................................................. +# test comment .......................................... +# test comment ................................................................ +# test comment

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice