2016-04-26 77 views
1

我想根據索引號重新定位字符串。使用awk/sed重定位字符串

索引號顯示在第一列,字符串顯示在第二列。

1  path_sparc_ifu_dec_104 
1  path_sparc_ifu_dec_105 
2  path_sparc_ifu_dec_63 
2  path_sparc_ifu_dec_92 
3  path_sparc_ifu_dec_39 
4  path_sparc_ifu_dec_43 
4  path_sparc_ifu_dec_71 
5  path_sparc_ifu_dec_103 
6  path_sparc_ifu_dec_99 
7  path_sparc_ifu_dec_55 
7  path_sparc_ifu_dec_75 
7  path_sparc_ifu_dec_93 

如果索引號是1,對應的字符串放在第一行。同時,索引號被刪除。

所需的輸出是:

path_sparc_ifu_dec_104 path_sparc_ifu_dec_105 
path_sparc_ifu_dec_63 path_sparc_ifu_dec_92 
path_sparc_ifu_dec_39 
path_sparc_ifu_dec_43 path_sparc_ifu_dec_71 
path_sparc_ifu_dec_103 
path_sparc_ifu_dec_99 
path_sparc_ifu_dec_55 path_sparc_ifu_dec_75 path_sparc_ifu_dec_93 

我覺得AWK/SED可以通過列讀取文件列和搬遷字符串,但我需要幫助!

我將不勝感激這個問題的任何幫助。

最佳,

Jaeyoung

+0

嘗試使用$ 1作爲數組索引,並附加空間和$ 2到它。在END規則中,打印出數組的每個元素。 – ReluctantBIOSGuy

+0

請看看:[我應該怎麼做當有人回答我的問題?](http://stackoverflow.com/help/someone-answers) – Cyrus

回答

2

使用awk

$ awk '{ 
    lines[$1] = ($1 in lines ? lines[$1] FS $2 : $2) 
    } END { 
    for (x in lines) print lines[x] 
}' file 
path_sparc_ifu_dec_104 path_sparc_ifu_dec_105 
path_sparc_ifu_dec_63 path_sparc_ifu_dec_92 
path_sparc_ifu_dec_39 
path_sparc_ifu_dec_43 path_sparc_ifu_dec_71 
path_sparc_ifu_dec_103 
path_sparc_ifu_dec_99 
path_sparc_ifu_dec_55 path_sparc_ifu_dec_75 path_sparc_ifu_dec_93 
+0

這工程!謝謝! –

1

我的版本:

$ awk '{if(prev==$1){printf(" ")}else{if(NR!=1){printf("\n")}};printf("%s",$2);prev=$1}END{printf("\n")}' file 
path_sparc_ifu_dec_104 path_sparc_ifu_dec_105 
path_sparc_ifu_dec_63 path_sparc_ifu_dec_92 
path_sparc_ifu_dec_39 
path_sparc_ifu_dec_43 path_sparc_ifu_dec_71 
path_sparc_ifu_dec_103 
path_sparc_ifu_dec_99 
path_sparc_ifu_dec_55 path_sparc_ifu_dec_75 path_sparc_ifu_dec_93 

因爲索引號進行排序,我們只是打印第二列,但不換行值。當索引號改變時,我們正在打印換行符。