【问题标题】:How to format the output based on the maximum column length如何根据最大列长度格式化输出
【发布时间】:2014-04-14 19:25:09
【问题描述】:

根据最大列长度格式化输出。我怎样才能做到这一点?

Shell 脚本或任何工具都可以。

输入

| date | ID | Typ | Actn |
| 11/29/13 | ID660011  | DP | A | 
| 11/29/13 | ID6600123  | DP | A | 

输出

| date    | ID       | Typ| Actn|
| 11/29/13| ID660011 | DP | A   | 
| 11/29/13| ID6600123| DP | A   | 

编辑:

如果我使用列 -t,这些是错误:
$ 列 -t -s'|' -o'|' 输入文件 .feature > 输入文件 _check.feature
列:非法选项 -- o
用法:列 [-tx] [-c 列] [-s sep] [文件...]

$ echo $SHELL /usr/local/bin/bash

$ column -t input_file .feature > input_file _check.feature 列: 行太长了

【问题讨论】:

  • 任何特定的外壳?
  • 你可以这样做:column -t file
  • @ScottHunter bash 更可取,但是,任何 shell 都可以
  • column -t -s'|' -o'|' inputfile 做你想做的事吗?
  • 你的线宽是否超过2k?

标签: shell unix formatting


【解决方案1】:

在你的 shell 终端上,试试这个:

$ awk '
{
   for(i=1;i<=NF;i++)
   printf("%-40s%c", $i, (i==NF) ? ORS : "")
}' FS=, file.txt

【讨论】:

    猜你喜欢
    • 2014-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-04
    • 1970-01-01
    相关资源
    最近更新 更多