linux 中sed -r选项的作用

 

-r:其中一项作用是保证字符为其本身字符; 没有-r字符具有正则意义;

如下例子:

001、  一下命令用于sed删除开头的空格

[root@pc1 test1]# ls
a.txt
[root@pc1 test1]# cat a.txt                         ## 测试文本
01 02 03 04
  05 06 07 08
    09 10 11 12
13 14 15 16
17 18 19 20
[root@pc1 test1]# sed -r 's/( *)(.*)/\2/' a.txt     ## 有 -r; 小括号自身就是字符;
01 02 03 04
05 06 07 08
09 10 11 12
13 14 15 16
17 18 19 20
[root@pc1 test1]# sed 's/( *)(.*)/\2/' a.txt         ## 无 -r; 小括号具有特殊意义;报错
sed: -e expression #1, char 14: invalid reference \2 on `s' command's RHS
[root@pc1 test1]# sed 's/\( *\)\(.*\)/\2/' a.txt     ## 无 -r; 需要对小括号进行转义
01 02 03 04
05 06 07 08
09 10 11 12
13 14 15 16
17 18 19 20

 。

 

posted @ 2024-02-20 11:38  小鲨鱼2018  阅读(445)  评论(0编辑  收藏  举报