轻松学会文本处理工具之二 linux sed命令(11)
代码如下: [root@jie1 ~]# cat myfile EOF hello world i am jie how are you EOF #先生成一个文件 [root@jie1 ~]# cat myfile hello world i am jie how are you [root@jie1 ~]# name=li #定义一个变
代码如下:
[root@jie1 ~]# cat >> myfile << EOF
> hello world
> i am jie
> how are you
> EOF #先生成一个文件
[root@jie1 ~]# cat myfile
hello world
i am jie
how are you
[root@jie1 ~]# name=li
#定义一个变量,且给变量赋值
[root@jie1 ~]# sed -i "s/jie/$name/" myfile
#把匹配jie的字符替换成变量的值
[root@jie1 ~]# cat myfile
hello world
i am li
how are you
[root@jie1 ~]# sed -i "$a $name" myfile
#当sed命令也有默认变量时,在去引用自己定义的变量会出现语法错误
sed: -e expression #1, char 3: extra characters after command
[root@jie1 ~]# sed -i '$a '$name'' myfile
#在引用自定义的变量时,sed语句必须用单引引住,然后把自定义的变量也用单引号引住
[root@jie1 ~]# cat myfile
hello world
i am li
how are you
li
[root@jie1 ~]#
sed的其它高级使用:
1)把正在用sed操作的文件的内容写到例外一个文件中
精彩图集
精彩文章