2014-09-04 37 views
-4

我已經使用vim打開一個文件替換(僅文本編輯器,在我的安裝Linux的),我需要搜索和替換BGA的所有實例CGA搜索和使用vim

我使用Ubuntu 12.04 LTS

該文件如下:

select comments,* from LOGS where barcode in ('BGA001248788','BGA000632039','BGA001270649','BGA000997171','BGA000997172','BGA000265968','BGA000265964','BGA000720466','BGA000720467','BGA002224291','BGA002224292','BGA000726647','BGA000609927','BGA000609928', 
'BGA000504740', 'BGA000702736','BGA000547632','BGA000583033','BGA000583034','BGA000632053','BGA000225618','BGA001248788','BGA000632039','BGA001270649','BGA000997171','BGA000997172','BGA000265968','BGA000265964','BGA000720466','BGA000720467','BGA002224291','BGA002224292','BGA000726647','BGA000609927', 
'BGA000609928','BGA000504740', 'BGA000702736','BGA000547632','BGA000583033','BGA000583034','BGA000632053','BGA000225618','BGA001248788','BGA000632039','BGA001270649','BGA000997171','BGA000997172','BGA000265968','BGA000265964','BGA000720466','BGA000720467','BGA002224291','BGA002224292','BGA000726647', 
'BGA000609927','BGA000609928','BGA000504740', 'BGA000702736','BGA000547632','BGA000583033','BGA000583034','BGA000632053','BGA000225618','BGA001248788','BGA000632039','BGA001270649','BGA000997171','BGA000997172','BGA000265968','BGA000265964','BGA000720466','BGA000720467','BGA002224291','BGA002224292','BGA000726647','BGA000609927','BGA000609928','BGA000504740', 'BGA000702736','BGA000547632','BGA000583033','BGA000583034','BGA000632053','BGA000225618','BGA001248788','BGA000632039','BGA001270649', 
'BGA000997171','BGA000997172','BGA000265968','BGA000265964','BGA000720466','BGA000720467','BGA002224291','BGA002224292','BGA000726647','BGA000609927','BGA000609928','BGA000504740', 'BGA000702736','BGA000547632','BGA000583033','BGA000583034','BGA000632053','BGA000225618') 

謝謝!

+0

您應該首先嚐試Google。有很多博客文章,Vim的幫助等等,關於搜索和替換。 – 2014-09-04 16:10:09

+1

[Regex for search and replace in vim]可能的重複(http://stackoverflow.com/questions/16063324/regex-for-search-and-replace-in-vim) – 2014-09-04 16:12:04

+0

我認爲'ed'標準編輯器應該也可以隨時使用。 – progo 2014-09-04 16:56:18

回答

2

要執行一攬子搜索並替換已在vi(m)中打開的緩衝區(文件),請執行以下操作:

1)按兩次退出鍵以確保您處於「準備好執行命令」 2)類型:

%s/BGA/CGA/gi 

2號線如下分解:

: - tells vi to expect a command 
%s - tells vi to use the substitute command 
/ - separator to tell vi that the search string is next 
BCA - the search string to search for 
/ - tells vi that the search string is terminated and to expect the replacement string 
CGA - the replacement string 
/ - tells vi that the replacement string is terminated 
g - g = global, all references in the buffer, if this is omitted then only the first instance from the cursor position is replaced 
i - i = ignore case 

希望這回答您的問題!

一旦你掌握了vi(m)就沒有回頭!

2

在正常模式下:%s/BGA/CGA/gc其中:

  • %意味着所有行
  • ssubstitute命令的別名
  • g裝置替換,每行多於1次
  • c詢問動作

這會問你,如果你想:

  • 更換(Y)
  • 通行證(N)
  • 更換所有(一)
  • 停止(Q)
  • 而更多

更多的解釋here或(在正常模式下):help subs