2015-10-21 25 views
0

我是新的Shell腳本。我的問題可能不是一個複雜的問題。有人能幫我解決嗎?使用Shell腳本切換表格內容

我有一個CSV文件中像這樣:

 
studentid|100|101|102|103 
studentname|abc|lkh|kjh|lkjh 
class|9|7|4|3 
marks|67|68|59|80 
location|hyd|chn|ban|hyd 

從上面的數據,我想有一個新的CSV文件象下面這樣:

 
studentid|marks|location 
100|67|hyd 
101|68|chn 
102|59|ban 
103|80|hyd 

回答

0
awk -va=studentid -vb=marks -vc=location 'BEGIN{FS=OFS="|"}{for(i=2;i<=NF;i++)x[$1,i]=$i}END{print a,b,c;for(i=2;i<=NF;i++){print x[a,i],x[b,i],x[c,i]}}' a 
studentid|marks|location 
100|67|hyd 
101|68|chn 
102|59|ban 
103|80|hyd 
+0

您能解釋一下您的答案嗎? –

+0

我的英語不好。我不能用英文解釋 – bian

0
awk -v 'Incl=studentid|marks|location' ' 
BEGIN { 
    FS = OFS = "|" 
    aLine[1] = Incl 
    ItemCount = 1 
    } 
$1 ~ Incl { 
    for (i = 2;i <= NF;i++) aLine[i]=aLine[i] (ItemCount++ > 1 ? "|" : "") $i 
    } 
END { 
    for(i = 1; i <= ItemCount; i++) print aLine[i] 
    } 
' YourFile 

通用腳本。只需提供想要的字段列表(與文件中的順序相同)以Incl變量作爲參數轉置