Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

awk to output table-like or excel-like columns in linux terminal?

I do a long pipe, that ends with ...| awk '{print $5"\t\t" $3"\t"$4}' in the Linux terminal. The columns are padded with tabs. The first column entries have different number of characters, so the second column results are not perfectly vertical. How to make the table look perfect?

enter image description here

like image 856
Ivan Avatar asked Sep 17 '12 12:09

Ivan


People also ask

What is awk '{ print $1 }'?

If you notice awk 'print $1' prints first word of each line. If you use $3, it will print 3rd word of each line.

Which character is used to identify the fields columns in awk?

You use a dollar sign (' $ ') to refer to a field in an awk program, followed by the number of the field you want. Thus, $1 refers to the first field, $2 to the second, and so on. (Unlike in the Unix shells, the field numbers are not limited to single digits.

How do I print multiple columns in Unix?

The `awk` command is one of many commands that can be used to print a range of columns from tabular data in Linux. The `awk` command is can be used directly from the terminal by executing the `awk` script file.


2 Answers

try to pipe the result to column -t:

...| awk '{print $5"\t\t" $3"\t"$4}'|column -t

hope it helps

like image 92
Kent Avatar answered Oct 19 '22 02:10

Kent


if your fields are tab separated the following one line script could print a table with cell borders

sed -e 's/\t/_|/g' table.txt |  column -t -s '_' | awk '1;!(NR%1){print "-----------------------------------------------------------------------";}'

Description            |value                 |Comment
-----------------------------------------------------------------------
Internal               |322                   |
-----------------------------------------------------------------------
External               |42515                 |
-----------------------------------------------------------------------
like image 34
LMC Avatar answered Oct 19 '22 02:10

LMC