2011-03-28 82 views
0

我使用一個命令在一個文件awk的結果問題

awk -F'[=,: ]' '{print /uid=/?$4:(/^telephoneN/)?$2:$3}' 1.txt 

輸出查找字符串和數字是一樣的東西

a 
b 
c 
d 

e 
f 
g 
t 

我想編寫一個文件2.XML這個輸出

<xml> 
<name>aaaa</name> 
<surname>bbbb</surname> 
... 
</xml> 
<xml> 
<name>eeee</name> 
<surname>ffff</surname> 
... 
</xml> 

我不知道如何管理awk的結果。 你能幫我嗎?

在此先感謝

+1

告訴我們1.txt的樣子 – 2011-03-28 13:45:54

回答

1

我會很高興地看到你的真實數據是什麼樣子,但考慮到你的輸出顯示4個領域和你的輸入顯示4個領域,這裏的基本思想。

awk 'BEGIN { 
    RS="" # make blank line between sets of data the RecordSep 
    FS="\n" # make each line as a field in the rec (like $1, $2 ...) 
} 
{ # this is the main loop, each record set is procssed here 
    printf("<xml>\n\t<name>%s</name>\n\t<surname>%s</surname>\n\t<Addr1>%s</Addr1>\n\t<Addr2>%s</Addr2>\n</xml>", 
     $1, $2, $3, $4) 
} ' 1.txt > 1.xml 

注:應該只有1喜歡你的記錄集之間的空白。

我希望這會有所幫助。

P.S.你似乎是一個新的用戶,如果你得到一個答案,可以幫助你請記得將其標記爲接受,或者給它一個+(或 - )作爲一個有用的答案。