2014-04-02 60 views
0
Unit Title  Class Title   File Name 

Unit Title1  Title1    Filename1 

Unit Title2  Title2    Filename2 
       Title3    Filename3 
       Title4    Filename4 
       Title5    Filename5 

Unit Title3  Title6    Filename6 
       Title7    Filename7 
       Title8    Filename8 
       Title9    Filename9 

Unit Title4  Title10    Filename10 
       Title11    Filename11 
       Title12    Filename12

我有大量TSV(製表符分隔值)文件,其結構像這樣。我試圖編寫一個bash腳本,可以將這些文件解析爲匹配的數組。這是空蕩蕩的線路,把我扔了一圈。我需要能夠列出課程標題,同時列出它所屬的「單元標題」。嘗試將TSV文件中的值解析爲2個匹配的Bash陣列

我已經可以將每個組合放到它們自己的數組中,但是我不能在「單元標題」中重複條目以與類標題對齊。有人能幫助我指出正確的方向嗎?謝謝!

+0

評論:請發表一些代碼,你已經有了。 – chillworld

+0

以及需要將哪些內容饋送到數組。 – BMW

回答

1

這我不清楚你想要的陣列看起來像什麼,但也許預處理輸入文件要填充所有列幫助:

awk -F'\t' -v OFS='\t' ' 
    $0 != "" { # process only non-empty lines 
     # If field 1 is empty, set it to the most recent unit title. 
    if ($1 != "") ut=$1; else $1=ut; 
     # Print the (rebuilt) line. 
    print 
    }' tsvfile 

這將導致類似( \t代表字面標籤),這應該使解析更容易:

Unit Title1\tTitle1\tFilename1 
Unit Title2\tTitle2\tFilename2 
Unit Title2\tTitle3\tFilename3 
Unit Title2\tTitle4\tFilename4 
Unit Title2\tTitle5\tFilename5 
Unit Title3\tTitle6\tFilename6 
Unit Title3\tTitle7\tFilename7 
... 
+0

完美!這正是我需要的。謝謝! – user3456429

+0

@ user3456429:我的榮幸;聽到那個消息很開心;我冒昧地在您的問題中將「CSV」更改爲「TSV」(製表符分隔值)。 – mklement0