sed -n使用变量

mac2024-04-04  34

通常我们使用sed打印文件某一行时会使用以下命令,比如打印第二行,

sed -n '2p' file

但有时脚本里需要对行号采用变量方式,这个后就有问题了,

[root@localhost ~]$ num=2;sed -n '$num p' file sed: -e expression #1, char 3: extra characters after command

此时需要对变量使用单引号,才能生效,

[root@localhost ~]$ cat a hello world [root@localhost ~]$ num=2;sed -n ''$num' p' a world
最新回复(0)