39

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%)
ktec
  • 2,453
  • 3
  • 26
  • 32
  • what exactly you are looking for? – ajreal Dec 10 '11 at 04:31
  • 1
    The "expand" command only works on tabs. It didn't work for me in my test (below) because when I cut and pasted your sample data, I got two spaces after "CHAR.L" and three spaces after "MXL.L". See below for details on how to do what you want using AWK instead. – ghoti Dec 14 '11 at 04:52

5 Answers5

61

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%)
Farbod Salamat-Zadeh
  • 19,687
  • 20
  • 75
  • 125
Amos Folarin
  • 2,059
  • 20
  • 18
  • to 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) – Aquarius Power Jul 24 '14 at 23:21
  • 2
    Nice, I had to use `-s ''` also for my file. (Type the tab in with `^V` then Tab.) – Wildcard Dec 11 '15 at 08:26
11

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%)
potong
  • 55,640
  • 6
  • 51
  • 83
2

Found it:

cat output.txt | expand --tabs=14
ktec
  • 2,453
  • 3
  • 26
  • 32
  • That 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. – ghoti Dec 12 '11 at 07:24
  • Thats interesting, its works perfectly under Ubuntu! What about if you increase the size? – ktec Dec 13 '11 at 21:40
  • 2
    **Comment by [jørgensen](http://stackoverflow.com/users/1091587/) (rejected edit):** You can rip out the useless use of `cat`. Simply run: `expand --tabs=14 – Anne Dec 14 '11 at 00:04
  • 2
    I 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. – Dave Jacoby Dec 19 '12 at 14:10
2

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.

ghoti
  • 45,319
  • 8
  • 65
  • 104
  • I'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 Dec 13 '11 at 21:42
  • Oh, the impression I had from your description pre-edit was that this was what you wanted. I'll add an alternative in an edit. – ghoti Dec 14 '11 at 04:35
0

if you're dealing with JSON data, column-layout will help: https://github.com/75lb/column-layout

Lloyd
  • 8,204
  • 2
  • 38
  • 53