40

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%)
2
  • what exactly you are looking for? Commented Dec 10, 2011 at 4: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. Commented Dec 14, 2011 at 4:52

5 Answers 5

65

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%)
Sign up to request clarification or add additional context in comments.

2 Comments

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)
Nice, I had to use -s '<tab>' also for my file. (Type the tab in with ^V then Tab.)
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%)

2 Comments

Thanks, but this still has columns miss-aligned which is what I'm trying to resolve.
Didn't help the OP, but exactly what I needed, thanks!
2

Found it:

cat output.txt | expand --tabs=14

4 Comments

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.
Thats interesting, its works perfectly under Ubuntu! What about if you increase the size?
Comment by jørgensen (rejected edit): You can rip out the useless use of cat. Simply run: expand --tabs=14 <output.txt
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.
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.

2 Comments

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.
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.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.