Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format console output in columns

I have the following text file:

[master]$ cat output.txt  CHAR.L  96.88   -6.75 (-6.49%) MXP.L   12.62   -1.00 (-7.41%) NEW.L   7.88    -0.75 (-8.57%) AGQ.L   17.75   -0.62 (-3.40%) RMP.L   13.12   -0.38 (-2.75%) RRR.L   3.35    -0.20 (-5.71%) RRL.L   7.95    -0.15 (-1.85%) SOU.L   1.73    -0.10 (-5.22%) YELL.L  5.47    -0.04 (-0.73%) AMC.L   9.75    -0.01 (-0.05%) PLU:USOP    95.40   0.00 (+0%) BP-.L   452.10  0.95 (+0.21%) SXX.L   29.00   1.50 (+5.41%) LLOY.L  26.78   1.64 (+6.52%) DES.L   23.62   2.25 (+10.34%) GKP.L   171.62  4.50 (+2.69%) XEL.L   83.75   5.00 (+6.33%) BARC.L  190.57  9.80 (+5.43%) RKH.L   251.62  12.00 (+5.02%) UKX.L   5529.21 45.44 (+0.83%) 

I would like to fix the alignment of the columns. Obviously I can import into a spreadsheet or something but I would like to remain within the terminal.

EDIT: Using expand I can achieve the desired result on Ubuntu, but is this the best way?

[master]$ cat output.txt | expand -t24 CHAR.L      96.88       -6.75 (-6.49%) AMC.L       9.75        -0.01 (-0.05%) PLU:USOP    95.40       0.00 (+0%) 
like image 658
ktec Avatar asked Dec 10 '11 04:12

ktec


People also ask

Which utility takes input from another command and formats output into columns?

It is very simple and easy to use command line utility. This command line utility converts the input file into multiple columns and you can convert the content into the columns based on any delimiter.

How do you console output a file?

the shortcut is Ctrl + Shift + S ; it allows the output to be saved as a text file, or as HTML including colors!

How do you split a column in Unix?

A nifty command called cut lets you select a list of columns or fields from one or more files. You must specify either the -c option to cut by column or -f to cut by fields. (Fields are separated by tabs unless you specify a different field separator with -d.


1 Answers

You can use the column command:

me@home$ column -t output.txt      CHAR.L    96.88    -6.75  (-6.49%)     MXP.L     12.62    -1.00  (-7.41%)     NEW.L     7.88     -0.75  (-8.57%)     AGQ.L     17.75    -0.62  (-3.40%)     RMP.L     13.12    -0.38  (-2.75%)     RRR.L     3.35     -0.20  (-5.71%)     RRL.L     7.95     -0.15  (-1.85%)     SOU.L     1.73     -0.10  (-5.22%)     YELL.L    5.47     -0.04  (-0.73%)     AMC.L     9.75     -0.01  (-0.05%)     PLU:USOP  95.40    0.00   (+0%)     BP-.L     452.10   0.95   (+0.21%)     SXX.L     29.00    1.50   (+5.41%)     LLOY.L    26.78    1.64   (+6.52%)     DES.L     23.62    2.25   (+10.34%)     GKP.L     171.62   4.50   (+2.69%)     XEL.L     83.75    5.00   (+6.33%)     BARC.L    190.57   9.80   (+5.43%)     RKH.L     251.62   12.00  (+5.02%)     UKX.L     5529.21  45.44  (+0.83%) 
like image 73
Amos Folarin Avatar answered Sep 26 '22 16:09

Amos Folarin