[nSLUG] Any Perl Experience around?

Hatem Nassrat hnassrat at gmail.com
Wed Jun 21 21:54:42 ADT 2017


Let me take a stab at explaining the regex ...

. Means any Charachter
{24} means repeated 24 times
Surrounding paranthesis means capture that pattern (since it is the first
it can be accessed with $1)
.* Means followed by any number of characters

This is replaced with $1 (ie the first 24 characters) then three ...

The switches to perl -lne mean something, not sure off the top, but I think
-e is for extending regex expressions. The rest may have something to do
with processing escape characters, not sure.

This is all passed to the column command, which splits on whitespace by
default and prints the output into tabular columns based on the width of
the display.

Without running this, my guess is the colors are adding a few characters
which are within the 24 characters captured and then are now whitespace
which are being mangled by the column command.

Hope this helps,

-- Hatem


On Jun 21, 2017 7:30 PM, "Joel Maxuel" <j.maxuel at gmail.com> wrote:

Hi all,

I encountered this example script in a StackExchange question[1]:

https://github.com/atav32/dotfiles/blob/master/bash_alias#L46

What it does is truncates the results (by file/directory) of an ls
statement to organize in predictable columns.

It was not long before I thought, "why not have colour?"

So I made a quick addition, and regretted it the rest of the night:

ls -F --color | perl -lne 's/(.{24}).*/$1... /s ; print' | column -x

Why so?  Colour is made up of additional/escape characters which the
close-out gets truncated and can leave symbols on-screen.  So I started to
analyze the enclosed Perl.  I see that a substitution is used (find > n
chars, replace with truncated version + ".... "), but I don't understand
the inner workings.

Essentially, looks like I want to trim a result if it is n characters wide
and no \e char to be found, or if with an \e, trim within it (which looks
to be 8 extra chars to the left and 4 to the right).

This may be a regex thing.

Any ideas?  Thanks.

[1] https://unix.stackexchange.com/questions/112335/can-i-
truncate-long-file-names-in-ls-listing

--
Cheers,
Joel Maxuel

"One should strive to achieve, not sit in bitter regret."
 - Ronan Harris / Mark Jackson

_______________________________________________
nSLUG mailing list
nSLUG at nslug.ns.ca
http://nslug.ns.ca/mailman/listinfo/nslug
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://nslug.ns.ca/pipermail/nslug/attachments/20170621/9eb54117/attachment.html>


More information about the nSLUG mailing list