如何在列中格式化控制台输出

kte*_*tec 31 linux terminal awk

我有以下文本文件:

[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%)
Run Code Online (Sandbox Code Playgroud)

我想修复列的对齐方式.显然我可以导入电子表格或其他东西,但我想留在终端内.

编辑:使用扩展我可以在Ubuntu上实现所需的结果,但这是最好的方法吗?

[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%)
Run Code Online (Sandbox Code Playgroud)

Amo*_*rin 51

列之类的解决方案就是你想要的.

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%)
Run Code Online (Sandbox Code Playgroud)

  • 很好,我不得不使用`-s'<tab>'`也用于我的文件.(使用`^ V`然后选项卡键入选项卡.) (2认同)

pot*_*ong 11

这可能对你有用:

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%)
Run Code Online (Sandbox Code Playgroud)

  • 没有帮助OP,但正是我需要的,谢谢! (3认同)

kte*_*tec 2

找到了:

cat output.txt | expand --tabs=14
Run Code Online (Sandbox Code Playgroud)

  • **[jørgensen](http://stackoverflow.com/users/1091587/) 的评论(拒绝编辑):** 您可以删除 `cat` 的无用用法。只需运行:“expand --tabs=14 &lt;output.txt” (2认同)
  • 我个人讨厌“无用地使用 cat”,因为我可能会通过数十个与问题无关的 shell 命令,并到达我想要使用“expand --tabs=14”或其他内容的位置。使用“cat file | Expand| grep | awk | sort”比使用“expand &lt; file | grep | awk | sort”更容易更改管道顺序。_让每个程序做好一件事。_猫做好一件事。 (2认同)