方便地格式化输出

方便地格式化输出

有什么方法可以格式化 grep 命令的输出以很好地对齐列吗?例如:

4TO/struct-2222332/coord-2222332.out:          FINAL HEAT OF FORMATION =       -299.61496 KCAL/MOL =   -1253.58899 KJ/MOL
2E/struct-1212123/coord-1212123.out:          FINAL HEAT OF FORMATION =       -299.61496 KCAL/MOL =   -1253.58901 KJ/MOL
OT4/struct-2222121/coord-2222121.out:          FINAL HEAT OF FORMATION =       -299.61497 KCAL/MOL =   -1253.58904 KJ/MOL
E3/struct-1312322/coord-1312322.out:          FINAL HEAT OF FORMATION =       -299.61497 KCAL/MOL =   -1253.58903 KJ/MOL  

我想为第一列分配 40 个空格,并从左侧对齐。同样,根据每列的预期最大大小对其他列执行相同的操作。

答案1

您可以使用该column命令(如果您使用的是 Linux)将数据格式化为制表符分隔的列。不过,我认为您无法为第一列和其他列指定不同的列宽。

bash-[24]$ column -t /tmp/output
4TO/struct-2222332/coord-2222332.out:  FINAL  HEAT  OF  FORMATION  =  -299.61496  KCAL/MOL  =  -1253.58899  KJ/MOL
2E/struct-1212123/coord-1212123.out:   FINAL  HEAT  OF  FORMATION  =  -299.61496  KCAL/MOL  =  -1253.58901  KJ/MOL
OT4/struct-2222121/coord-2222121.out:  FINAL  HEAT  OF  FORMATION  =  -299.61497  KCAL/MOL  =  -1253.58904  KJ/MOL
E3/struct-1312322/coord-1312322.out:   FINAL  HEAT  OF  FORMATION  =  -299.61497  KCAL/MOL  =  -1253.58903  KJ/MOL

您的另一个选择是解析文件、拆分字段并使用 和 自行处理awkprintf()

bash-[54]$ awk '{printf "%-40s",$1 ; $1=""; printf "%-s\n",$0}' /tmp/output
4TO/struct-2222332/coord-2222332.out:    FINAL HEAT OF FORMATION = -299.61496 KCAL/MOL = -1253.58899 KJ/MOL
2E/struct-1212123/coord-1212123.out:     FINAL HEAT OF FORMATION = -299.61496 KCAL/MOL = -1253.58901 KJ/MOL
OT4/struct-2222121/coord-2222121.out:    FINAL HEAT OF FORMATION = -299.61497 KCAL/MOL = -1253.58904 KJ/MOL
E3/struct-1312322/coord-1312322.out:     FINAL HEAT OF FORMATION = -299.61497 KCAL/MOL = -1253.58903 KJ/MOL

相关内容