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;

}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值