2

I use column command to pretty print a table, e.g

column -s ':' -t < /etc/passwd

It works but all separator are replaced with spaces, e.g

_applepay  *  260  260  applepay Account  /var/db/applepay  /usr/bin/false
_hidd      *  261  261  HID Service User  /var/db/hidd      /usr/bin/false

I need something like this

_applepay | * | 260 | 260 | applepay Account | /var/db/applepay  | /usr/bin/false

Any ideas?

I've just read the manual of column and there's no such option

3

In looking over the answer from don_crissti and comparing that with the various output formats from the perl table modules, I created this ready-to-expand perl code so that it produced output similar to the non-BSD code column:

#!/usr/bin/env perl

# @(#) p5       Demonstrate framework for non-bsd util-linux:column.

use strict;
use warnings;
use Text::FormatTable;

my ($input_separator)  = ":";
my ($output_separator) = " | ";
my ( $rows, @a, @my_be, $back_end );

# Data rows from colon-separated data, e.g. passwd-format file.
while (<>) {
  chomp;
  @a = split /$input_separator/;
  push @$rows, [@a];
}

# Prepare and print the table.
my $t = Text::FormatTable->new(
  join( $output_separator, ('l') x @{ $rows->[0] } ) );
$t->head( @{ $rows->[0] } );
$t->row( @{ $rows->[$_] } ) for 1 .. @$rows - 1;
print $t->render;

exit(0);

when run with the data2 file noted in the other answer, produces:

$ ./p5 data2 
login     | password | UID | GID | name        | home             | shell            
daemon    | x        | 1   | 1   | daemon      | /usr/sbin        | /usr/sbin/nologin
bin       | x        | 2   | 2   | bin         | /bin             | /usr/sbin/nologin
_applepay | *        | 260 | 260 | applepay    | /var/db/applepay | /usr/bin/false   
_hidd     | *        | 261 | 261 | HID Service | /var/db/hidd     | /usr/bin/false  

Best wishes ... cheers, drl

3

If you're using column from util-linux you can specify the columns delimiter via

   -o, --output-separator string

so e.g.

column -s ':' -o ' | ' -t /etc/passwd

will print something like

root        | x | 0    | 0    | root      | /root           | /bin/zsh
bin         | x | 1    | 1    | bin       | /bin            | /usr/bin/nologin
daemon      | x | 2    | 2    | daemon    | /               | /usr/bin/nologin
mail        | x | 8    | 12   | mail      | /var/spool/mail | /usr/bin/nologin
whatever    | x | 14   | 11   | whatever  | /srv/stuff      | /usr/bin/nologin
  • Good to know: Debian (and Debian-based distros) install column from bsdmainutils (so no -o on Debian), SUSE and RedHat install the same from util-linux (supports -o). See also: askubuntu.com/questions/622889/… – sborsky Oct 21 '17 at 23:29
  • @don_crissti, thanks for the explanation. I was unable to compile util-linux to get the non-BSD version of column for a Debian install -- too many libraries needed to update. I may expand a perl code to be an almost work-alike. – drl Oct 22 '17 at 18:32
  • Another solution that I tried was to make a static executable of the non-BSD source of column from Fedora and transfer it to Debian. There are 2 utilities that have been noted to do that: statifier (free), and Ermine (cost). I tried statifier and it failed even with turning off ASLR. The Ermine (Light, trial) seemed to work. These are executables that include library resources in the modified executable, for more details, see nnc3.com/mags/LM10/Magazine/Archive/2009/105/050-052_statifier/… They are BIG executables, static 2.8 MB vs shared 28 KB. – drl Oct 23 '17 at 11:47
0

Use sed to replace the separator.

sed 's/:/ | /g' /etc/passwd
0

The column command cannot do the replacement on delimiter, it will only take delimiter as an option to make beautifier the output. You may need to have an extra sed command to add | later.

column -s ':' -t < /etc/passwd| sed -E 's/  ([^ ])/\t|\1/g'

Just more beautifier (center).

column -s ':' -t < /etc/passwd| sed -E 's/  ([^ ])/  |  \1/g'
0

Apologies for the long post.

There are a number of perl modules that perform table formatting. Here is a comparison of some of them. The data is on file data2. The script snippet is:

# print-like-echo; print-line-with-visual-space.
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }

FILE=${1-data2}

pl " Data file $FILE:"
head $FILE

pl " Results with column:"
column -s ':' -t $FILE

pl " Results from column | sed:"
column -s ':' -t < $FILE | sed -E 's/ ([^ ])/ | \1/g'

pl " Results of sed | column:"
sed 's/:/ | /g' $FILE | column -s ':' -t

./p4 $FILE

The perl code is about 30 lines of perl (and a few comments) on file p4. It will require that some modules be installed:

#!/usr/bin/env perl

# @(#) p4       Demonstrate various table formatters on password file.

use strict;
use warnings;

use Text::Table::Any;

my ( $rows, @a, @my_be, $back_end );

# Data rows from passwd-format file.
while (<>) {
  chomp;
  @a = split /:/;
  push @$rows, [@a];
}

@my_be = ("Text::Table::Tiny");

# push @my_be, 'Text::Table::TinyColor';
# push @my_be, 'Text::Table::TinyColorWide';
# push @my_be, 'Text::Table::TinyWide';
push @my_be, 'Text::Table::Org';
push @my_be, 'Text::Table::CSV';

# push @my_be, 'Text::Table::HTML';
# push @my_be, 'Text::Table::HTML::DataTables';
push @my_be, 'Text::Table::Paragraph';

# push @my_be, 'Text::ANSITable';
push @my_be, 'Text::ASCIITable';
push @my_be, 'Text::FormatTable';

# push @my_be, 'Text::MarkdownTable';
push @my_be, 'Text::Table';
push @my_be, 'Text::TabularDisplay';

# push @my_be, 'Text::TestForError';

foreach $back_end (@my_be) {
  print "\n\n----\n";
  print " Backend table processor = $back_end\n";
  print "\n";
  print Text::Table::Any::table(
    rows       => $rows,
    header_row => 1,
    backend    => $back_end,

  );
}

The results of running ./s1 data2 are:

-----
 Data file data2:
login:password:UID:GID:name:home:shell
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
_applepay:*:260:260:applepay:/var/db/applepay:/usr/bin/false
_hidd:*:261:261:HID Service:/var/db/hidd:/usr/bin/false

-----
 Results with column:
login      password  UID  GID  name         home              shell
daemon     x         1    1    daemon       /usr/sbin         /usr/sbin/nologin
bin        x         2    2    bin          /bin              /usr/sbin/nologin
_applepay  *         260  260  applepay     /var/db/applepay  /usr/bin/false
_hidd      *         261  261  HID Service  /var/db/hidd      /usr/bin/false

-----
 Results from column | sed:
login      | password  | UID  | GID  | name         | home              | shell
daemon     | x         | 1    | 1    | daemon       | /usr/sbin         | /usr/sbin/nologin
bin        | x         | 2    | 2    | bin          | /bin              | /usr/sbin/nologin
_applepay  | *         | 260  | 260  | applepay     | /var/db/applepay  | /usr/bin/false
_hidd      | *         | 261  | 261  | HID | Service  | /var/db/hidd      | /usr/bin/false

-----
 Results of sed | column:
login | password | UID | GID | name | home | shell
daemon | x | 1 | 1 | daemon | /usr/sbin | /usr/sbin/nologin
bin | x | 2 | 2 | bin | /bin | /usr/sbin/nologin
_applepay | * | 260 | 260 | applepay | /var/db/applepay | /usr/bin/false
_hidd | * | 261 | 261 | HID Service | /var/db/hidd | /usr/bin/false


----
 Backend table processor = Text::Table::Tiny

+-----------+----------+-----+-----+-------------+------------------+-------------------+
| login     | password | UID | GID | name        | home             | shell             |
+-----------+----------+-----+-----+-------------+------------------+-------------------+
| daemon    | x        | 1   | 1   | daemon      | /usr/sbin        | /usr/sbin/nologin |
| bin       | x        | 2   | 2   | bin         | /bin             | /usr/sbin/nologin |
| _applepay | *        | 260 | 260 | applepay    | /var/db/applepay | /usr/bin/false    |
| _hidd     | *        | 261 | 261 | HID Service | /var/db/hidd     | /usr/bin/false    |
+-----------+----------+-----+-----+-------------+------------------+-------------------+


----
 Backend table processor = Text::Table::Org

| login     | password | UID | GID | name        | home             | shell             |
|-----------+----------+-----+-----+-------------+------------------+-------------------|
| daemon    | x        | 1   | 1   | daemon      | /usr/sbin        | /usr/sbin/nologin |
| bin       | x        | 2   | 2   | bin         | /bin             | /usr/sbin/nologin |
| _applepay | *        | 260 | 260 | applepay    | /var/db/applepay | /usr/bin/false    |
| _hidd     | *        | 261 | 261 | HID Service | /var/db/hidd     | /usr/bin/false    |


----
 Backend table processor = Text::Table::CSV

"login","password","UID","GID","name","home","shell"
"daemon","x","1","1","daemon","/usr/sbin","/usr/sbin/nologin"
"bin","x","2","2","bin","/bin","/usr/sbin/nologin"
"_applepay","*","260","260","applepay","/var/db/applepay","/usr/bin/false"
"_hidd","*","261","261","HID Service","/var/db/hidd","/usr/bin/false"


----
 Backend table processor = Text::Table::Paragraph

login: daemon
password: x
UID: 1
GID: 1
name: daemon
home: /usr/sbin
shell: /usr/sbin/nologin

login: bin
password: x
UID: 2
GID: 2
name: bin
home: /bin
shell: /usr/sbin/nologin

login: _applepay
password: *
UID: 260
GID: 260
name: applepay
home: /var/db/applepay
shell: /usr/bin/false

login: _hidd
password: *
UID: 261
GID: 261
name: HID Service
home: /var/db/hidd
shell: /usr/bin/false



----
 Backend table processor = Text::ASCIITable

.---------------------------------------------------------------------------------------.
| login     | password | UID | GID | name        | home             | shell             |
+-----------+----------+-----+-----+-------------+------------------+-------------------+
| daemon    | x        |   1 |   1 | daemon      | /usr/sbin        | /usr/sbin/nologin |
| bin       | x        |   2 |   2 | bin         | /bin             | /usr/sbin/nologin |
| _applepay | *        | 260 | 260 | applepay    | /var/db/applepay | /usr/bin/false    |
| _hidd     | *        | 261 | 261 | HID Service | /var/db/hidd     | /usr/bin/false    |
'-----------+----------+-----+-----+-------------+------------------+-------------------'


----
 Backend table processor = Text::FormatTable

login    |password|UID|GID|name       |home            |shell            
daemon   |x       |1  |1  |daemon     |/usr/sbin       |/usr/sbin/nologin
bin      |x       |2  |2  |bin        |/bin            |/usr/sbin/nologin
_applepay|*       |260|260|applepay   |/var/db/applepay|/usr/bin/false   
_hidd    |*       |261|261|HID Service|/var/db/hidd    |/usr/bin/false   


----
 Backend table processor = Text::Table

login     password UID GID name        home             shell            
daemon    x          1   1 daemon      /usr/sbin        /usr/sbin/nologin
bin       x          2   2 bin         /bin             /usr/sbin/nologin
_applepay *        260 260 applepay    /var/db/applepay /usr/bin/false   
_hidd     *        261 261 HID Service /var/db/hidd     /usr/bin/false   


----
 Backend table processor = Text::TabularDisplay

+-----------+----------+-----+-----+-------------+------------------+-------------------+
| login     | password | UID | GID | name        | home             | shell             |
+-----------+----------+-----+-----+-------------+------------------+-------------------+
| daemon    | x        | 1   | 1   | daemon      | /usr/sbin        | /usr/sbin/nologin |
| bin       | x        | 2   | 2   | bin         | /bin             | /usr/sbin/nologin |
| _applepay | *        | 260 | 260 | applepay    | /var/db/applepay | /usr/bin/false    |
| _hidd     | *        | 261 | 261 | HID Service | /var/db/hidd     | /usr/bin/false    |
+-----------+----------+-----+-----+-------------+------------------+-------------------+

An interesting feature is that modules Text::ASCIITable and Text::Table right-justify fields of numbers.

This is on a system like:

OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.9 (jessie) 
bash GNU bash 4.3.30
perl 5.20.2

Best wishes ... cheers, drl

0
awk 'BEGIN{FS=":";OFS=" | "}{$1=$1;print$0}' /etc/passwd
  • Does not align the columns! – fpmurphy Oct 22 '17 at 5:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.