在awk中格式化输出文本并在文档中打开

时间:2017-01-03 15:52:57

标签: python bash perl awk formatting

我有这样的数据集:

BRCC    hete    1   15869   105A
BRAC    he  1   1799967 956G
BCAS    he  2   7334543 369AFVC
RCA he  4   9534262 7806-14
RCA he  5   144848  1114A
RA  he  5   206118  52A
BCAVV   he  5   543304  3807TCD
BCA hoo 1   106091515   4308TDDSC
BCA hoo 1   206075  4563A
BCA hoo 1   799917  2612CDSA
BCA hoo 2   206076  513G
BCA hoom    3   16941   3113A

数据集有6列,与标签分开。我想格式化文本并添加标题。我的解决方案是:

awk -v OFS="\t" 'BEGIN{printf "%s\t %s\t %s\t %s\t %s\t \n" ,"TEST","HEADER","CLASS","CLASS2","SVGH" pritnf "\n" "__________________________________________________"} {printf "%s\t %s\t %s\t %s\t %s\t \n", $1,$2,$3,$4,$5}' in.txt > out.doc

但我需要将输出保存到文档并在LibreOffice中打开。但输出看起来像这样:

enter image description here

有没有办法形成文本以保持标题下的列 - 更好的可读性?像这样:

enter image description here

注意:我会帮助解决任何问题。

1 个答案:

答案 0 :(得分:4)

听起来这就是你所需要的:

$ awk 'BEGIN{print "TEST","HEADER","CLASS","CLASS2","SVGH"} 1' file | column -t
TEST   HEADER  CLASS  CLASS2     SVGH
BRCC   hete    1      15869      105A
BRAC   he      1      1799967    956G
BCAS   he      2      7334543    369AFVC
RCA    he      4      9534262    7806-14
RCA    he      5      144848     1114A
RA     he      5      206118     52A
BCAVV  he      5      543304     3807TCD
BCA    hoo     1      106091515  4308TDDSC
BCA    hoo     1      206075     4563A
BCA    hoo     1      799917     2612CDSA
BCA    hoo     2      206076     513G
BCA    hoom    3      16941      3113A

然后在LibreOffice中使用恒定宽度字体来显示它。要强调标题行:

$ awk 'BEGIN{print "TEST","HEADER","CLASS","CLASS2","SVGH"} 1' file | column -t |
    awk '{print} NR==1{gsub(/./,"_"); print}'
TEST   HEADER  CLASS  CLASS2     SVGH
_____________________________________
BRCC   hete    1      15869      105A
BRAC   he      1      1799967    956G
BCAS   he      2      7334543    369AFVC
RCA    he      4      9534262    7806-14
RCA    he      5      144848     1114A
RA     he      5      206118     52A
BCAVV  he      5      543304     3807TCD
BCA    hoo     1      106091515  4308TDDSC
BCA    hoo     1      206075     4563A
BCA    hoo     1      799917     2612CDSA
BCA    hoo     2      206076     513G
BCA    hoom    3      16941      3113A