Tab Seperated text converted to tabular format with border
-
21-08-2019 - |
Question
I needed to convert a Tab Seperated Text file into a tabular format as follows
File content
ID<TAB>WorkId<TAB>Date
0<TAB>W-1230699600000<TAB>2008-12-31
1<TAB>W-1233378000000<TAB>2009-01-31
needs to become
+--------+-----------------+-------------+
| ID | WorkId | Date |
+--------+-----------------+-------------+
| 0 | W-1230699600000 | 2008-12-31 |
| 1 | W-1233378000000 | 2009-01-31 |
+--------+-----------------+-------------+
Solution
I ended up using awk to do this. Here is what I did.
cat /tmp/1.txt | awk -F'\t' '
BEGIN {
fieldSize=5;
for (i=1; i < fieldSize; i++) {
fLength[i]=0;
}
}
{
for (i=1; i <= fieldSize; i++) {
if (fLength[i] < length($i)) {
fLength[i] = length($i)
}
}
line[NR]=$0
}
END {
for (i=1; i <= NR; i++) {
split(line[i], values, "\t")
format="| "
arguments=""
seperator="+"
for (j=1; j <= fieldSize; j++) {
format=format"%-"fLength[j]"s | "
for (c=0; c <= fLength[j]; c++) seperator=seperator"-"
seperator=seperator"-+"
arguments=arguments" \""values[j]"\""
}
if (i == 1) print seperator;
printCommand="printf \""format"\n\" "arguments
system(printCommand)
if (i == 1) print seperator;
}
print seperator;
}
'
OTHER TIPS
Use Perl's formatter:
#!/usr/bin/perl
open(MYFILE, ">myfile.txt") or die "File does not exist!";
my $id, $workid, $date;
print MYFILE
"+-----+----------+---------------+\n" .
"|ID |WorkId |Date |\n" .
"+-----+----------+---------------+\n";
format MYFILE =
|@<<<<|@<<<<<<<<<|@<<<<<<<<<<<<<<|
$id $workid $date
+-----+----------+---------------+
.
while(<>)
{
next if (m/^ID/);
($id,$workid,$date) = split(/\t/);
write MYFILE;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow