Linux 如何在列中格式化控制台输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8454277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to format console output in columns
提问by ktec
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 alignmentof 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?
编辑:使用 expand 我可以在 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%)
回答by ktec
Found it:
找到了:
cat output.txt | expand --tabs=14
回答by potong
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%)
回答by ghoti
This prints your file in three columns using awk, since that what you asked about:
这使用 awk 在三列中打印您的文件,因为您询问的是:
cat output.txt | \
awk -v cols=3 '{printf("%-44s", awk '{ printf("%-10s%8s%8s %s\n", , , , ); }' < output.txt
)} 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).
如果您的输出始终使用单个 TAB 来分隔列,那么正如您所见, expand 将适用于您。但是“awk”更适合这种任务,因为它可以让您更完全地控制格式。awk(默认情况下)将所有空格视为字段分隔符(因此“”和“^I”和“^I”都是单个字段分隔符)。
After the update to the question, it seems that this is what you're looking for:
更新问题后,这似乎是您要查找的内容:
awk '{ printf("%-10s%8.2f%8.2f %s\n", , , , ); }' < output.txt
If you want to restrict the format a little more, you could use:
如果你想限制更多的格式,你可以使用:
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%)
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.
如果您愿意,您可以花哨并控制最后一列的格式,但我怀疑这是另一个问题的主题。
回答by Amos Folarin
You can use the column
command:
您可以使用以下column
命令:
回答by Lloyd
if you're dealing with JSON data, column-layout
will help: https://github.com/75lb/column-layout
如果您正在处理 JSON 数据,column-layout
将会有所帮助:https: //github.com/75lb/column-layout