linux vi 替换字符串,vi字符串替换命令
vi 編輯器提供簡單的字符串替換命令,在末行模式下可以使用替換命令,其命令格式如下:
[range]s/s1/s2/ [option]
[range] 表示檢索范圍,省略時表示當前行。下面是一些檢索范圍的例子。
1,10表示從第 1 行到 10 行。
%表示整個文件,同1, $。
. ,$從當前行到文件尾。
s 為替換命令。
s1 要被替換的串,s2 為替換的串。
option 表示選項:
/g表示在全局文件中進行替換。
/c表示在每次替換之前需要用戶進行確認。
省略時僅對每行第一個匹配串進行替換。
下面實例演示了字符串替換命令,將第 10~22 行的 printf 標識符替換為 PRINT。
1) 在 vi 編輯器中編輯以下代碼:
int main(void)
{
struct stat buf; /*存儲文件狀態信息*/
if(stat("s1", &buf) == -1) {
perror ("fail to stat");
exit(1);
}
printf("permission : %d\n", buf.st_mode);
printf("inode number : %d\n", buf.st_ino);
printf("device number : %d\n", buf.st_dev);
printf("r-device number : %d\n", buf.st_rdev);
printf("link : %d\n", buf.st_nlink);
printf("uid : %d\n", buf.st_uid);
printf("gid : %d\n", buf.st_gid);
printf("file size : %d\n", buf.st_size);
printf("access time : %d\n", buf.st_atime);
printf("motify time : %d\n", buf.st_mtime);
printf("change time : %d\n", buf.st_ctime);
printf("buf size : %d\n", buf.st_blksize);
printf("block size : %d\n", buf.st_blocks);
return 0;
}
2) 在末行模式下輸入以下替換命令:
10,22s/printf/PRINT/g
屏幕顯示如下:
int main(void)
{
struct stat buf; /*存儲文件狀態信息*/
if(stat("s1", &buf) == -1) {
perror ("fail to stat");
exit(1);
}
PRINTF("permission : %d\n", buf.st_mode);
PRINTF("inode number : %d\n", buf.st_ino);
PRINTF("device number : %d\n", buf.st_dev);
PRINTF("r-device number : %d\n", buf.st_rdev);
PRINTF("link : %d\n", buf.st_nlink);
PRINTF("uid : %d\n", buf.st_uid);
PRINTF("gid : %d\n", buf.st_gid);
PRINTF("file size : %d\n", buf.st_size);
PRINTF("access time : %d\n", buf.st_atime);
PRINTF("motify time : %d\n", buf.st_mtime);
PRINTF("change time : %d\n", buf.st_ctime);
PRINTF("buf size : %d\n", buf.st_blksize);
PRINTF("block size : %d\n", buf.st_blocks);
return 0;
}
vi 編輯器在執行替換命令指令時,可以進行簡單的模式匹配,其匹配模式如下:
\
替換以 word 開始的單詞的 word 部分。例如:
%s/\
表示將以 abc 開頭的單詞替換為以 cde 開頭。
下例演示了這種替換操作,更改 stat 結構的變量名,將整個文件中的 buf 替換為 stat_buf。
1) 在 vi 編輯器中編輯以下代碼:
int main(void)
{
struct stat buf; /*存儲文件狀態信息*/
if(stat("s1", &buf) == -1) {
perror ("fail to stat");
exit(1);
}
printf("permission : %d\n", buf.st_mode);
printf("inode number : %d\n", buf.st_ino);
printf("device number : %d\n", buf.st_dev);
printf("r-device number : %d\n", buf.st_rdev);
printf("link : %d\n", buf.st_nlink);
printf("uid : %d\n", buf.st_uid);
printf("gid : %d\n", buf.st_gid);
printf("file size : %d\n", buf.st_size);
printf("access time : %d\n", buf.st_atime);
printf("motify time : %d\n", buf.st_mtime);
printf("change time : %d\n", buf.st_ctime);
printf("buf size : %d\n", buf.st_blksize);
printf("block size : %d\n", buf.st_blocks);
return 0;
}
2) 在末行模式下輸入命令:
%s/\
屏幕顯示如下:
int main(void)
{
struct stat stat_buf; /*存儲文件狀態信息*/
if(stat("s1", &stat_buf) == -1) {
perror ("fail to stat");
exit(1);
}
printf("permission : %d\n", stat_buf.st_mode);
printf("inode number : %d\n", stat_buf.st_ino);
printf("device number : %d\n", stat_buf.st_dev);
printf("r-device number : %d\n", stat_buf.st_rdev);
printf("link : %d\n", stat_buf.st_nlink);
printf("uid : %d\n", stat_buf.st_uid);
printf("gid : %d\n", stat_buf.st_gid);
printf("file size : %d\n", stat_buf.st_size);
printf("access time : %d\n", stat_buf.st_atime);
printf("motify time : %d\n", stat_buf.st_mtime);
printf("change time : %d\n", stat_buf.st_ctime);
printf("stat_buf size : %d\n", stat_buf.st_blksize);
printf("block size : %d\n", stat_buf.st_blocks);
return 0;
}
總結
以上是生活随笔為你收集整理的linux vi 替换字符串,vi字符串替换命令的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 自编自适应中值滤波器
- 下一篇: 中值滤波器的matlab实现