2013-04-24 76 views
1

我有以下數據,嘗試了所有我可以,但沒能成功......GNU桑達符合豎條改變

Las Vegas Nevada 89102|US|Katrina Roetteler 
zhen fo shan guang dong 528318|CN|tu min jung 
Zurich CH8022|CH|Kevin M Pearl 
Cheltenham GL52 8XX|UK|Kevin M Pearl 
Melbourne Vic 3000|AU|brent chandler 
Bratislava 821 03|Slovak Republic|SUSTRIK MARTIN MGR. 
Zurich 8032|CH|David Graf 
Tokyo 178-0063|JP|takahiro ichihashi 

我需要改變這樣的:

Las Vegas Nevada|89102|US|Katrina Roetteler 
zhen fo shan guang dong|528318|CN|tu min jung 
Zurich |CH8022|CH|Kevin M Pearl 
Cheltenham|GL52 8XX|UK|Kevin M Pearl 
Melbourne Vic|3000|AU|brent chandler 
Bratislava |821 03|Slovak Republic|SUSTRIK MARTIN MGR. 
Zurich|8032|CH|David Graf 
Tokyo|178-0063|JP|takahiro ichihashi 

基本上在另一列(全球範圍內我的客戶的不同類型的郵編)中具有郵政編碼/郵政編碼,並且郵政編碼/郵政編碼位於第一個垂直條之前。

謝謝!

+0

你可能將不得不使用單獨的正則表達式爲每個國家以符合該國的郵政代碼 – antlersoft 2013-04-24 20:45:05

+1

你的標題和標籤建議您要使用SED這一點,而不是(比如說)AWK或Perl。這是爲什麼?你有一個已經部分工作的sed腳本? – ruakh 2013-04-24 20:45:32

回答

2

使用SED:

sed 's/^\([A-Za-z ]*\) \([-A-Z 0-9]*|\)/\1|\2/' input 

輸出

Las Vegas Nevada|89102|US|Katrina Roetteler 
zhen fo shan guang dong|528318|CN|tu min jung 
Zurich |CH8022|CH|Kevin M Pearl 
Cheltenham|GL52 8XX|UK|Kevin M Pearl 
Melbourne Vic|3000|AU|brent chandler 
Bratislava |821 03|Slovak Republic|SUSTRIK MARTIN MGR. 
Zurich|8032|CH|David Graf 
Tokyo|178-0063|JP|takahiro ichihashi 
+1

這個伎倆! – bsteo 2013-04-24 20:58:15

1

下面的正則表達式做的:

sed 's/ \([A-Z]*[0-9][0-9]*\)/\|\1/' 
1

的東西,讓你開始使用AWK:

{ 
    for (i = 1; i < NF; i++) { 
     if ($i~/\|/) { 
      if ($(i-1)~/[0-9]/) { 
       $(i-1) = "|"$(i-1) 
      } else { 
      $i = "|"$i 
      } 
      break 
     } 
    } 
} 
{print} 

輸出:

Las Vegas Nevada |89102|US|Katrina Roetteler 
zhen fo shan guang dong |528318|CN|tu min jung 
Zurich |CH8022|CH|Kevin M Pearl 
Cheltenham |GL52 8XX|UK|Kevin M Pearl 
Melbourne Vic |3000|AU|brent chandler 
Bratislava |821 03|Slovak Republic|SUSTRIK MARTIN MGR. 
Zurich |8032|CH|David Graf 
Tokyo |178-0063|JP|takahiro ichihashi