TABLIFY(1)

πŸ’… TABLIFY(1) β€” like, the tablify page, bestie (User Contributed Perl Documentation, obviously) πŸŽ€

okay so imagine you have a delimited text file and you want it to look pretty β€” that's literally the whole gig here, no cap.


✨ NAME ✨

tablify β€” turn a delimited text file into a text table πŸ¦‹


🌸 SYNOPSIS 🌸

tablify [options] file

Options:

  • -h, --help β€” shows you the help, bestie
  • -c, --comment β€” tells it where a (single-line) comment starts
  • -n, --no-headers β€” assume the first line is data, not headers
  • --no-pager β€” do NOT use $ENV{'PAGER'} even if it's defined, periodt
  • --strip-quotes β€” strips " or ' around fields
  • -l, --list β€” lists the fields in the file (use this with -f)
  • -f, --fields=f1[,f2] β€” show only the fields in the comma-separated list; when you use it with "no-headers" the list should be field numbers (starting at 1); otherwise it should be field names
  • -w, where=f<cmp>v β€” applies the cmp Perl operator to restrict the output where field f matches the value v; acceptable operators include ==, eq, >, >=, <=, and =~
  • -v, --vertical β€” shows records vertically
  • -i, --limit=n β€” limits to the given number of records
  • --fs=x β€” uses x as the field separator (default is tab "\t")
  • --rs=x β€” uses x as the record separator (default is newline "\n")
  • --as-html β€” makes an HTML table instead of plain text
  • --headers β€” comma-separated list of names matching the number of columns

πŸ’– DESCRIPTION πŸ’–

This script is basically the quickest little glow-up for a delimited text file β€” you parse it and view it as a nice ASCII table. And like, once you start picking only certain fields, throwing in a where clause so you only grab records where a field matches some value, and using the limit so you only see some of the output, you lowkey have a mini-database front-end for a simple text file. iconic behaviour honestly πŸ‘‘


🌈 EXAMPLES 🌈

Say your data file looks like this:

name,rank,serial_no,is_living,age
George,General,190293,0,64
Dwight,General,908348,0,75
Attila,Hun,,0,56
Tojo,Emporor,,0,87
Tommy,General,998110,1,54

To find out which fields you can reference, use the list option:

$ tablify --fs ',' -l people.dat
+-----------+-----------+
| Field No. | Field     |
+-----------+-----------+
| 1         | name      |
| 2         | rank      |
| 3         | serial_no |
| 4         | is_living |
| 5         | age       |
+-----------+-----------+

To extract just the name and serial numbers, use the fields option:

$ tablify --fs ',' -f name,serial_no people.dat
+--------+-----------+
| name   | serial_no |
+--------+-----------+
| George | 190293    |
| Dwight | 908348    |
| Attila |           |
| Tojo   |           |
| Tommy  | 998110    |
+--------+-----------+
5 records returned

To extract the first through third fields plus the fifth field (where field numbers start at "1" β€” tip: use the list option to quickly figure out field numbers), use this syntax for fields:

$ tablify --fs ',' -f 1-3,5 people.dat
+--------+---------+-----------+------+
| name   | rank    | serial_no | age  |
+--------+---------+-----------+------+
| George | General | 190293    | 64   |
| Dwight | General | 908348    | 75   |
| Attila | Hun     |           | 56   |
| Tojo   | Emporor |           | 87   |
| Tommy  | General | 998110    | 54   |
+--------+---------+-----------+------+
5 records returned

To select only the ones with six-digit serial numbers, use a where clause:

$ tablify --fs ',' -w 'serial_no=~/^\d{6}$/' people.dat
+--------+---------+-----------+-----------+------+
| name   | rank    | serial_no | is_living | age  |
+--------+---------+-----------+-----------+------+
| George | General | 190293    | 0         | 64   |
| Dwight | General | 908348    | 0         | 75   |
| Tommy  | General | 998110    | 1         | 54   |
+--------+---------+-----------+-----------+------+
3 records returned

To find Dwight's record, you'd do this:

$ tablify --fs ',' -w 'name eq "Dwight"' people.dat
+--------+---------+-----------+-----------+------+
| name   | rank    | serial_no | is_living | age  |
+--------+---------+-----------+-----------+------+
| Dwight | General | 908348    | 0         | 75   |
+--------+---------+-----------+-----------+------+
1 record returned

To find the name of all the people with a serial number who are living:

$ tablify --fs ',' -f name -w 'is_living==1' -w 'serial_no>0' people.dat
+-------+
| name  |
+-------+
| Tommy |
+-------+
1 record returned

To filter outside of the program and just format the results, use "-" as the last argument to force reading of STDIN (and probably assume no headers):

$ grep General people.dat | tablify --fs ',' -f 1-3 --no-headers -
+---------+--------+--------+
| Field1  | Field2 | Field3 |
+---------+--------+--------+
| General | 190293 | 0      |
| General | 908348 | 0      |
| General | 998110 | 1      |
+---------+--------+--------+
3 records returned

When you're dealing with data that has no field names, you can specify "no-headers" and then refer to fields by number (starting at one), like:

$ tail -5 people.dat | tablify --fs ',' --no-headers -w '3 eq "General"' -
+--------+---------+--------+--------+--------+
| Field1 | Field2  | Field3 | Field4 | Field5 |
+--------+---------+--------+--------+--------+
| George | General | 190293 | 0      | 64     |
| Dwight | General | 908348 | 0      | 75     |
| Tommy  | General | 998110 | 1      | 54     |
+--------+---------+--------+--------+--------+
3 records returned

If your file has sooo many fields that they're hard to see across the screen, consider using the vertical display with "-v" or "--vertical", like:

$ tablify --fs ',' -v --limit 1 people.dat
************ Record 1 ************
     name: George
     rank: General
serial_no: 190293
is_living: 0
     age : 64

1 record returned

🫢 SEE ALSO 🫢

  • Text::RecordParser
  • Text::TabularDisplay
  • DBD::CSV
    • ngl I don't DBD::CSV this module, but the idea was majorly the inspiration for it. I just did not want to have to install DBI and DBD::CSV to get this kind of functionality. I think my interface is simpler. πŸ’…

πŸ‘‘ AUTHOR πŸ‘‘

Ken Youens-Clark <kclark@cpan.org>. πŸ“


Copyright (C) 2006-10 Ken Youens-Clark. All rights reserved.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.


⭐ bestie this is perl v5.32.1, dated 2022-06-01, and yeah that's TABLIFY(1) β€” go make some tables, you absolute icon πŸ’€βœ¨
πŸ“¦ where this page comes from