2014-10-30 61 views
-2

我想尋求你的文本處理幫助。Unix文本處理

我有一個文件,該文件是在以下格式

TITLE : Test1 
    TYPE : X 
    TYPE : Y 
    TYPE : Z 
    PROC : 490 
    LIN : 524 
    LIN : 660 

我想獲得這個處理如下

Test1, X/Y/Z, 490, 524/660 

這只不過是截取換行用逗號分隔符和合並同一列1的值與/。我不擔心第1列沒有打印。

+0

這個問題有什麼不對嗎? – Sathy 2014-10-30 02:24:18

+1

問題沒問題,但我想有些人關閉它,因爲你沒有顯示你已經嘗試過。 'awk'{a [NR] = $ 3} END {print a [1]「,」a [2]「/」a [3]「/」a [4]「,」a [5]「,」a [6]「/」a [7]}'文件' – Jotne 2014-10-30 07:55:06

回答

1

這對我有用。

awk '{ 
    # Set ORS to "/" or "," based on whether the current line has the same first field as the previous line 
    ORS=($1==lastkey)?"/":"," 
    # Save our last seen first field 
    lastkey=$1 
    # Print out ORS and the new third field except on the first line where we just print the third field. 
    printf "%s%s", (NR==1)?"":ORS, $3 
} 
END { 
    # Print out a trailing newline. 
    printf "\n" 
}'