下面有一個文件cut.csv,內容如下:
[[email protected] ~]$ cat cut.csv
golonglee, [email protected] youname
golonglee, [email protected] youname
golonglee, [email protected] youname
golonglee, [email protected] youname
golonglee, [email protected] youname
golonglee, [email protected] youname
golonglee, [email protected] youname
golonglee, [email protected] youname
golonglee, [email protected] youname
golonglee, [email protected] youname
golonglee, [email protected] youname
golonglee, [email protected] youname
golonglee, [email protected] youname
現在有一個需求要把這個文件中的所有逗號,替換成冒號:在vi編輯器下使用命令
:n1,n2/word1/word2/g命令失效,也就是
:1,21/,/:/g失效,使用轉移字元\,也就是
:1,21/\,/\:/g失效,後來找到一種方法可行,如下
:%s+,+:+可行
工具/原料
:n1,n2/word1/word2/g
vi編輯器
方法/步驟
命令列開啟cut.csv檔案,vi cut.csv,並設定開啟行號:set nu 內容如下:
1 golonglee, [email protected] youname
2 golonglee, [email protected] youname
3 golonglee, [email protected] youname
4 golonglee, [email protected] youname
5 golonglee, [email protected] youname
6 golonglee, [email protected] youname
7 golonglee, [email protected] youname
8 golonglee, [email protected] youname
9 golonglee, [email protected] youname
10 golonglee, [email protected] youname
11 golonglee, [email protected] youname
12 golonglee, [email protected] youname
13 golonglee, [email protected] youname
14 golonglee, [email protected] youname
15 golonglee, [email protected] youname
16 golonglee, [email protected] youname
17 golonglee, [email protected] youname
18 golonglee, [email protected] youname
19 golonglee, [email protected] youname
20 golonglee, [email protected] youname
21 golonglee, [email protected] youname
輸入英文的冒號: 然後輸入 nonu即可取消顯示行號
之前的經驗裡面說過使用cut -f3 -d, +檔名,可以擷取某一段
擷取後的效果
用vi cut.tab開啟要編輯的檔案cut.tab
輸入如下命令:%s+,+:+並回車
在命令模式下,輸入
:%s+,+:+並回車,看到cut.csv中的逗號,都已經被冒號:替換,內容如下:
1 golonglee: [email protected] youname
2 golonglee: [email protected] youname
3 golonglee: [email protected] youname
4 golonglee: [email protected] youname
5 golonglee: [email protected] youname
6 golonglee: [email protected] youname
7 golonglee: [email protected] youname
8 golonglee: [email protected] youname
9 golonglee: [email protected] youname
10 golonglee: [email protected] youname
11 golonglee: [email protected] youname
12 golonglee: [email protected] youname
13 golonglee: [email protected] youname
14 golonglee: [email protected] youname
15 golonglee: [email protected] youname
16 golonglee: [email protected] youname
17 golonglee: [email protected] youname
18 golonglee: [email protected] youname
19 golonglee: [email protected] youname
20 golonglee: [email protected] youname
21 golonglee: [email protected] youname
~
~
21 substitutions on 21 lines