2016-02-16 82 views
-1

我試圖在多個分隔符和組分割線的輸出到我可以重新排列各個元素。我在運行pkg_info命令的BSD系統上。輸出看起來像這樣。AWK分組和劈裂多個定界符

yaesu-0.13nb1  Control interface for Yaesu FT-890 HF transceiver 
skk-jisyo-cdb-201212 Dictionary collection for SKK 
dbskkd-cdb-2.00nb1 SKK dictionary server based on cdb 
libchewing-0.2.7 The intelligent phonetic input method library 
skk-jisyo-201212 Dictionary collection for SKK 
autoconf-2.69nb2 Generates automatic source code configuration scripts 
pkg-config-0.28  System for managing library compile/link flags 
python27-2.7.5  Interpreted, interactive, object-oriented programming language 

包名稱總是包含字母和數字的版本是連接到名字的最後一個條目和描述總是由至少一個空格隔開。最複雜的例子是,「skk-jisyo-cdb」是軟件包名稱。 「201212」是版本,「SKK字典集合」是描述。

我需要的版本從包名稱,就留下了包名的完整與「 - 」留在這,而分裂版本信息從那裏,使它自己的元素。最後,我需要將描述作爲第三個要素保持完整。

我覺得無論是AWK或sed的是能夠做到這一點,但還沒有到能夠組的元素都能正常。任何幫助深表感謝!

這裏有幾個到目前爲止我已經試過的東西:

pkg_info -a | awk -F'[[:space:]]*' '{print $1}' | awk -F- '{$NF=" "$NF;sub(/ /,"-")}1' 

輸出:

yaesu- 0.13nb1 
skk-jisyo cdb 201212 
dbskkd-cdb 2.00nb1 
libchewing- 0.2.7 
skk-jisyo 201212 
autoconf- 2.69nb2 
pkg-config 0.28 
python27- 2.7.5 

而且

pkg_info -a | awk 'BEGIN{FS="-| ";OFS="\t"}{print $1$2}' 

輸出:

yaesu0.13nb1 
skkjisyo 
dbskkdcdb 
libchewing0.2.7 
skkjisyo 
autoconf2.69nb2 
pkgconfig 
python272.7.5 

我已經能夠使用2個命令分離出包名和版本,但這不是我想要/需要的。這些僅供參考。 這將讓我的版本本身:

pkg_info -a | awk -F'[[:space:]]*' '{print $1}' | awk -F- '{print $NF }' 

這將讓我包的名稱本身:

pkg_info -a | awk -F'[[:space:]]*' '{print $1}' | sed 's/\(.*\)\(-.*\)/\1/g' 

我需要我的最終輸出是爲$pkgname\t$version\t$description\n這將由被分隔\t標籤 擁有最複雜的例子輸出是: skk-jisyo-cdb\t201212\tDictionary collection for SKK\n

+0

@Ed莫頓,我更增添了幾分澄清 – rayray84

+0

對不起埃德,我跳槍,並沒有給你什麼你要找的一次。我只是再次修改它。我認爲,通過你給我的例子,我可以使它工作。我現在就試一試。謝謝 – rayray84

回答

2

您沒有提供足夠的細節,以確保但是這可能是你想要什麼:

$ sed -r 's/([^[:blank:]]+)-([^[:blank:]]+)[[:blank:]]+/\1\t\2\t/' file 
yaesu 0.13nb1 Control interface for Yaesu FT-890 HF transceiver 
skk-jisyo-cdb 201212 Dictionary collection for SKK 
dbskkd-cdb  2.00nb1 SKK dictionary server based on cdb 
libchewing  0.2.7 The intelligent phonetic input method library 
skk-jisyo  201212 Dictionary collection for SKK 
autoconf  2.69nb2 Generates automatic source code configuration scripts 
pkg-config  0.28 System for managing library compile/link flags 
python27  2.7.5 Interpreted, interactive, object-oriented programming language 

$ awk -v OFS='\t' '{ pkg=ver=$1; sub(/-[^-]+$/,"",pkg); sub(/.*-/,"",ver); sub(/[^[:space:]]+[[:space:]]+/,""); print pkg, ver, $0}' file 
yaesu 0.13nb1 Control interface for Yaesu FT-890 HF transceiver 
skk-jisyo-cdb 201212 Dictionary collection for SKK 
dbskkd-cdb  2.00nb1 SKK dictionary server based on cdb 
libchewing  0.2.7 The intelligent phonetic input method library 
skk-jisyo  201212 Dictionary collection for SKK 
autoconf  2.69nb2 Generates automatic source code configuration scripts 
pkg-config  0.28 System for managing library compile/link flags 
python27  2.7.5 Interpreted, interactive, object-oriented programming language 

將分隔符從tab更改爲任何你想要的。

+1

這適用於我需要的!謝謝@Ed Morton!我只需要將OFS更改爲OFS ='\\ t' – rayray84

0

您可以使用默認的分隔和分割功能上1場然後你只需要附加字段分隔和分裂的第一個字段中的最後一項:

awk '{n=split($1, a, "-"); $1=$1 FS a[n]}1' 
+0

這給了我一個輸出:'yaesu-0.13nb1 0.13nb1 Yaesu FT-890 HF收發器的控制接口 skk-jisyo-cdb-201212 201212 SKK字典集合 dbskkd-cdb-2.00nb1 2.00nb1 SKK字典服務器基於cdb libchewing-0.2.7 0.2.7智能語音輸入法庫 skk-jisyo-201212 201212 SKK字典集合 autoconf-2.69nb2 2.69nb2生成自動源代碼配置腳本 pkg-config-0.28 0.28系統用於管理庫編譯/鏈接標誌 python27-2.7.5 2.7.5解釋的交互式面向對象編程語言 – rayray84

+1

@ rayray84:它不是你想要的嗎?下次寫一個更明確的問題。 –

+0

抱歉卡西米爾,感謝您的輸入!我能夠得到我需要與埃德答案 – rayray84