How to format console output in columns
Solution 1
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%)
Solution 2
This might work for you:
pr -tw132 -3 output.txt
CHAR.L 96.88 -6.75 (-6.49%) SOU.L 1.73 -0.10 (-5.22%) DES.L 23.62 2.25 (+10.34%)
MXP.L 12.62 -1.00 (-7.41%) YELL.L 5.47 -0.04 (-0.73%) GKP.L 171.62 4.50 (+2.69%)
NEW.L 7.88 -0.75 (-8.57%) AMC.L 9.75 -0.01 (-0.05%) XEL.L 83.75 5.00 (+6.33%)
AGQ.L 17.75 -0.62 (-3.40%) PLU:USOP 95.40 0.00 (+0%) BARC.L 190.57 9.80 (+5.43%)
RMP.L 13.12 -0.38 (-2.75%) BP-.L 452.10 0.95 (+0.21%) RKH.L 251.62 12.00 (+5.02%)
RRR.L 3.35 -0.20 (-5.71%) SXX.L 29.00 1.50 (+5.41%) UKX.L 5529.21 45.44 (+0.83%)
RRL.L 7.95 -0.15 (-1.85%) LLOY.L 26.78 1.64 (+6.52%)
Solution 3
This prints your file in three columns using awk, since that what you asked about:
cat output.txt | \
awk -v cols=3 '{printf("%-44s",$0)} NR%cols==0 {print ""} END {print ""}'
EDIT:
If your output is consistently using single TABs to separate columns, then expand will work for you, as you've seen. Bu "awk" is more suited to this sort of task, as it will let you control formatting more completely. Awk (by default) considers all whitespace to be field separators (thus " " and "^I" and " ^I" are all single field separators).
After the update to the question, it seems that this is what you're looking for:
awk '{ printf("%-10s%8s%8s %s\n", $1, $2, $3, $4); }' < output.txt
If you want to restrict the format a little more, you could use:
awk '{ printf("%-10s%8.2f%8.2f %s\n", $1, $2, $3, $4); }' < output.txt
You could get fancy and control the format of the last column if you felt like it, but I suspect that's the topic of another question.
Solution 4
Found it:
cat output.txt | expand --tabs=14
ktec
Updated on July 09, 2022Comments
-
ktec almost 2 years
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%)
-
ghoti over 12 yearsThat the
expand
command only takes a "--tabs" option in Linux. In FreeBSD, you have to use the "-t" option. Also, are you sure about this solution? This doesn't work for me, using your data. -
ktec over 12 yearsThats interesting, its works perfectly under Ubuntu! What about if you increase the size?
-
ktec over 12 yearsI've tried this and it just gives me three columns, which I can get from using pr. What I would like is to fix the alignment of the columns.
-
ktec over 12 yearsThanks, but this still has columns miss-aligned which is what I'm trying to resolve.
-
Anne over 12 yearsComment by jørgensen (rejected edit): You can rip out the useless use of
cat
. Simply run:expand --tabs=14 <output.txt
-
ghoti over 12 yearsOh, the impression I had from your description pre-edit was that this was what you wanted. I'll add an alternative in an edit.
-
Dave Jacoby over 11 yearsI personally hate "useless use of cat", because I might pipe through dozens of shell commands which are not germane to the question and get to where I want to use "expand --tabs=14" or whatever. And changing pipe order is easier with "cat file | expand| grep | awk | sort" than with "expand < file | grep | awk | sort". Make each program do one thing well. cat does one thing well.
-
Aquarius Power almost 10 yearsto work more or less well, I had to do this:
ls -1 |column -c `tput cols` |column -t
(ls -1 is just a replacement for a simple 1 column long list of items) -
Wildcard over 8 yearsNice, I had to use
-s '<tab>'
also for my file. (Type the tab in with^V
then Tab.) -
shuckc over 8 yearsDidn't help the OP, but exactly what I needed, thanks!