1TABLIFY(1) User Contributed Perl Documentation TABLIFY(1)
2
3
4
6 tablify - turn a delimited text file into a text table
7
9 tablify [options] file
10
11 Options:
12
13 -h|--help Show help
14 -c|--comment Define the beginning of a (single-line) comment
15 -n|--no-headers Assume first line is data, not headers
16 --no-pager Do not use $ENV{'PAGER'} even if defined
17 --strip-quotes Strip " or ' around fields
18 -l|--list List the fields in the file (for use with -f)
19 -f|--fields=f1[,f2] Show only fields in comma-separated list;
20 when used in conjunction with "no-headers"
21 the list should be field numbers (starting at 1);
22 otherwise, should be field names
23 -w|where=f<cmp>v Apply the "cmp" Perl operator to restrict output
24 where field "f" matches the value "v"; acceptable
25 operators include ==, eq, >, >=, <=, and =~
26 -v|--vertical Show records vertically
27 -i|--limit=n Limit to given number of records
28 --fs=x Use "x" as the field separator
29 (default is tab "\t")
30 --rs=x Use "x" as the record separator
31 (default is newline "\n")
32 --as-html Create an HTML table instead of plain text
33 --headers Comma-separated list of names matching
34 the number of columns
35
37 This script is essentially a quick way to parse a delimited text file
38 and view it as a nice ASCII table. By selecting only certain fields,
39 employing a where clause to only select records where a field matches
40 some value, and using the limit to only see some of the output, you
41 almost have a mini-database front-end for a simple text file.
42
44 Given a data file like this:
45
46 name,rank,serial_no,is_living,age
47 George,General,190293,0,64
48 Dwight,General,908348,0,75
49 Attila,Hun,,0,56
50 Tojo,Emporor,,0,87
51 Tommy,General,998110,1,54
52
53 To find the fields you can reference, use the list option:
54
55 $ tablify --fs ',' -l people.dat
56 +-----------+-----------+
57 | Field No. | Field |
58 +-----------+-----------+
59 | 1 | name |
60 | 2 | rank |
61 | 3 | serial_no |
62 | 4 | is_living |
63 | 5 | age |
64 +-----------+-----------+
65
66 To extract just the name and serial numbers, use the fields option:
67
68 $ tablify --fs ',' -f name,serial_no people.dat
69 +--------+-----------+
70 | name | serial_no |
71 +--------+-----------+
72 | George | 190293 |
73 | Dwight | 908348 |
74 | Attila | |
75 | Tojo | |
76 | Tommy | 998110 |
77 +--------+-----------+
78 5 records returned
79
80 To extract the first through third fields and the fifth field (where
81 field numbers start at "1" -- tip: use the list option to quickly
82 determine field numbers), use this syntax for fields:
83
84 $ tablify --fs ',' -f 1-3,5 people.dat
85 +--------+---------+-----------+------+
86 | name | rank | serial_no | age |
87 +--------+---------+-----------+------+
88 | George | General | 190293 | 64 |
89 | Dwight | General | 908348 | 75 |
90 | Attila | Hun | | 56 |
91 | Tojo | Emporor | | 87 |
92 | Tommy | General | 998110 | 54 |
93 +--------+---------+-----------+------+
94 5 records returned
95
96 To select only the ones with six serial numbers, use a where clause:
97
98 $ tablify --fs ',' -w 'serial_no=~/^\d{6}$/' people.dat
99 +--------+---------+-----------+-----------+------+
100 | name | rank | serial_no | is_living | age |
101 +--------+---------+-----------+-----------+------+
102 | George | General | 190293 | 0 | 64 |
103 | Dwight | General | 908348 | 0 | 75 |
104 | Tommy | General | 998110 | 1 | 54 |
105 +--------+---------+-----------+-----------+------+
106 3 records returned
107
108 To find Dwight's record, you would do this:
109
110 $ tablify --fs ',' -w 'name eq "Dwight"' people.dat
111 +--------+---------+-----------+-----------+------+
112 | name | rank | serial_no | is_living | age |
113 +--------+---------+-----------+-----------+------+
114 | Dwight | General | 908348 | 0 | 75 |
115 +--------+---------+-----------+-----------+------+
116 1 record returned
117
118 To find the name of all the people with a serial number who are living:
119
120 $ tablify --fs ',' -f name -w 'is_living==1' -w 'serial_no>0' people.dat
121 +-------+
122 | name |
123 +-------+
124 | Tommy |
125 +-------+
126 1 record returned
127
128 To filter outside of program and simply format the results, use "-" as
129 the last argument to force reading of STDIN (and probably assume no
130 headers):
131
132 $ grep General people.dat | tablify --fs ',' -f 1-3 --no-headers -
133 +---------+--------+--------+
134 | Field1 | Field2 | Field3 |
135 +---------+--------+--------+
136 | General | 190293 | 0 |
137 | General | 908348 | 0 |
138 | General | 998110 | 1 |
139 +---------+--------+--------+
140 3 records returned
141
142 When dealing with data lacking field names, you can specify "no-
143 headers" and then refer to fields by number (starting at one), e.g.:
144
145 $ tail -5 people.dat | tablify --fs ',' --no-headers -w '3 eq "General"' -
146 +--------+---------+--------+--------+--------+
147 | Field1 | Field2 | Field3 | Field4 | Field5 |
148 +--------+---------+--------+--------+--------+
149 | George | General | 190293 | 0 | 64 |
150 | Dwight | General | 908348 | 0 | 75 |
151 | Tommy | General | 998110 | 1 | 54 |
152 +--------+---------+--------+--------+--------+
153 3 records returned
154
155 If your file has many fields which are hard to see across the screen,
156 consider using the vertical display with "-v" or "--vertical", e.g.:
157
158 $ tablify --fs ',' -v --limit 1 people.dat
159 ************ Record 1 ************
160 name: George
161 rank: General
162 serial_no: 190293
163 is_living: 0
164 age : 64
165
166 1 record returned
167
169 • Text::RecordParser
170
171 • Text::TabularDisplay
172
173 • DBD::CSV
174
175 Although I don't DBD::CSV this module, the idea was much the
176 inspiration for this. I just didn't want to have to install DBI
177 and DBD::CSV to get this kind of functionality. I think my
178 interface is simpler.
179
181 Ken Youens-Clark <kclark@cpan.org>.
182
184 Copyright (C) 2006-10 Ken Youens-Clark. All rights reserved.
185
186 This program is free software; you can redistribute it and/or modify it
187 under the terms of the GNU General Public License as published by the
188 Free Software Foundation; version 2.
189
190 This program is distributed in the hope that it will be useful, but
191 WITHOUT ANY WARRANTY; without even the implied warranty of
192 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
193 General Public License for more details.
194
195
196
197perl v5.36.0 2022-07-22 TABLIFY(1)