2017-01-03 98 views
-1

我有一個數據集合是這樣的:格式化輸出文本和開放於文件

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

注:我會apperciate不僅任何幫助awk的解決方案。

+1

首先確定每個列所需的空間,然後在Perl中,你可以添加空格這樣的'perl的-E「的printf‘%-10s%-10s \ n’,‘BRCC’ ,「hoom」'' –

+0

從我的awk腳本輸出第一個文檔。第二張圖片僅由自己在LibreOffice – Paul

+0

@HåkonHægland編輯,感謝您的回覆。有可能編寫完整的語法? Perl我真的不知道。 – Paul

回答

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 
+1

非常感謝Ed,這看起來比我想象的要容易得多。有什麼辦法可以在標題下面保留「_____」這一行嗎? – Paul

+1

'...列-t | sed 2i $(printf「_%。0s」{1..42})' – karakfa

+1

我添加了一個隨後的awk命令來強調標題行。 –