Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Aligning delimited columns

by neilwatson (Priest)
on May 31, 2013 at 13:50 UTC ( #1036247=perlquestion: print w/replies, xml ) Need Help??
neilwatson has asked for the wisdom of the Perl Monks concerning the following question:

I have some tables like this:
blah blah ;; yada yada ;; etc etc
A much longer field ;; yada yada ;; etc etc
Each field can be any width. I want to align the columns including the delimiters. A command like column removes the delimiter. I want to keep the delimiter. I thought this would have been done already, but I can't find anything. So I'm turning to Perl. Again I thought someone would have done this before, but my search has turned up empty. Do I have to roll my own?

UPDATE: You can scrub this node. This works:

cat file.txt |sed -e 's/;;/;;@@/g' | column -s '@@' -t
It adds a second, temporary delimiter.

Neil Watson

Replies are listed 'Best First'.
Re: Aligning delimited columns
by hbm (Hermit) on May 31, 2013 at 14:19 UTC

    No need to pipe the file into sed (i.e., why read the file twice?)

    sed 's/;;/;;@@/g' file.txt | column -s '@@' -t
Re: Aligning delimited columns
by ww (Archbishop) on May 31, 2013 at 17:53 UTC

    Keep the delimiter? See perldoc -f split

    #!/usr/bin/perl use 5.016; use Data::Dumper; my $text = "blah blah ;; yada yada ;; etc etc \n A much longer field ;; yada yada ;; etc etc"; my @arr = split /(;;)/,$text; # capture delimiter say Dumper @arr;

    Is this what you mean you say you want to keep the delimiter?

    C:\> $VAR1 = 'blah blah '; $VAR2 = ';;'; $VAR3 = ' yada yada '; $VAR4 = ';;'; $VAR5 = ' etc etc A much longer field '; $VAR6 = ';;'; $VAR7 = ' yada yada '; $VAR8 = ';;'; $VAR9 = ' etc etc';

    If you didn't program your executable by toggling in binary, it wasn't really programming!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1036247]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2017-06-24 04:20 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (556 votes). Check out past polls.