2014-10-18 26 views
0
cat raw.txt 

Name country IP Cost 
sam us 10.10.10.10 $250 
jack India 10.10.10.12 $190 
joy Australia 10.10.10.13 $230 
christ canada 10.10.10.15 $190 
jackson africa 10.10.10.20 $230 

我需要像表列表輸出四柱和四列,即姓名國籍IP成本如何用awk,sed的,grep來獲取文本文件的內容表或削減Linux終端提示

http://res.cloudinary.com/dzy8bgton/image/upload/v1413617325/Screenshot_from_2014-10-18_12_35_11_h6wjsu.png

請任何人都可以幫助我。

+0

您可以使用'R'閱讀並使用'xtable'包生成乳膠代碼。 http://cran.r-project.org/web/packages/xtable/vignettes/xtableGallery.pdf – user1436187 2014-10-18 07:34:45

+0

你想在哪裏輸出它?在外殼或其他地方? – 2014-10-18 07:38:16

+0

我希望只在shell中使用表格。 – 2014-10-18 07:50:04

回答

1

這裏是一個老同學的答案:-)

#!/bin/sh 
# use tbl|nroff to make an ASCII table 
# use sed to change multiple spaces into a single tab for tbl(1) 
sed 's/ */\t/g' < raw.txt | awk ' 
BEGIN { 
    print ".TS" # beginning of table 
    print "allbox;" # allbox format 
    print "c s s s" # Table name format - centered and spanning 4 columns 
    print "lb lb lb lb" # bold column headers 
    print "l l l l." # table with 4 left justified columns. "." means repeat for next line 
    print "My Table" # Table name 
} 
{print} # print each line of 4 values 
END { 
    print ".TE" # end of table 
}' | tbl | nroff -Tdumb 

產生

┌─────────────────────────────────────────┐ 
│    My Table     │ 
├────────┬───────────┬─────────────┬──────┤ 
│Name │ country │ IP   │ Cost │ 
├────────┼───────────┼─────────────┼──────┤ 
│sam  │ us  │ 10.10.10.10 │ $250 │ 
├────────┼───────────┼─────────────┼──────┤ 
│jack │ India  │ 10.10.10.12 │ $190 │ 
├────────┼───────────┼─────────────┼──────┤ 
│joy  │ Australia │ 10.10.10.13 │ $230 │ 
├────────┼───────────┼─────────────┼──────┤ 
│christ │ canada │ 10.10.10.15 │ $190 │ 
├────────┼───────────┼─────────────┼──────┤ 
│jackson │ africa │ 10.10.10.20 │ $230 │ 
└────────┴───────────┴─────────────┴──────┘ 
0

你可以嘗試column命令:

column -t file 
Name  country IP   Cost 
sam  us   10.10.10.10 $250 
jack  India  10.10.10.12 $190 
joy  Australia 10.10.10.13 $230 
christ canada  10.10.10.15 $190 
jackson africa  10.10.10.20 $230 
相關問題