Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Aligning delimited columns

by neilwatson (Curate)
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
watson-wilson.ca

Comment on Aligning delimited columns
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 (Bishop) 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:\> 1036247.pl $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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1036247]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2015-07-31 01:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (274 votes), past polls