使用sed在指定行尾插入字符

时间:2022-04-07 15:31:09

例如在server行的行尾插入指定字符:

nc10@your-5554c55be4 ~
$ cat sheet
server 127.0.0.1 localhost connected yes 12-3-08
server 127.0.0.1 localhost connected no 12-3-08
server 127.0.0.1 localhost connected yes 14-9-09
server 127.0.0.1 localhost connected yes 15-9-09
server 127.0.0.1 localhost connected no 16-9-09

nc10@your-5554c55be4 ~
$ sed '/^server.*$/s//& test/g' sheet
server 127.0.0.1 localhost connected yes 12-3-08 test
server 127.0.0.1 localhost connected no 12-3-08 test
server 127.0.0.1 localhost connected yes 14-9-09 test
server 127.0.0.1 localhost connected yes 15-9-09 test
server 127.0.0.1 localhost connected no 16-9-09 test

如果文件这样也可以,因为 /^server/ 限定了 只是行头是
server 才在行尾插入字符

user@minix-nb:~$ cat sheet
remote 127.0.0.1 localhost connected yes 12-3-08
server 127.0.0.1 localhost connected no 12-3-08
remote 127.0.0.1 localhost connected yes 14-9-09
server 127.0.0.1 localhost connected yes 15-9-09
remote 127.0.0.1 localhost connected no 16-9-09

user@minix-nb:~$ sed '/^server.*$/s//& test/g' sheet
remote 127.0.0.1 localhost connected yes 12-3-08
server 127.0.0.1 localhost connected no 12-3-08 test
remote 127.0.0.1 localhost connected yes 14-9-09
server 127.0.0.1 localhost connected yes 15-9-09 test
remote 127.0.0.1 localhost connected no 16-9-09