目录
只有自定义变量 无sed内置变量 的情况
自定义变量 sed内置变量 同时存在 的情况
只有自定义变量 无sed内置变量 的情况
在sed条件中是不认识变量取值的
sed '/$x/d' test
所以要想它能够识别变量
sed "/$x/d/" test
方法简单就是把"单引号"变成"双引号"
自定义变量 sed内置变量 同时存在 的情况
此情况下 如果用双引号 sed内置变量将不能识别 比如 $p 是最后一行
直接用双引号无效 需要分开使用 自定义变量用双引号 内置变量用单引号
例如 $line 是自定义变量
复制代码
1cat test.txt | sed -n "$line"',$p'
具体实例如下
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25[root@localhost ~]# cat test.txt 123 456 789 abc def [root@localhost ~]# cat test.txt | sed -n '2,$p' 456 789 abc def [root@localhost ~]# line=2 [root@localhost ~]# echo $line 2 [root@localhost ~]# cat test.txt | sed -n '$line,$p' sed: -e expression #1, char 3: extra characters after command [root@localhost ~]# cat test.txt | sed -n "$line,$p" sed: -e expression #1, char 2: unexpected `,' [root@localhost ~]# cat test.txt | sed -n "$line"',$p' 456 789 abc def [root@localhost ~]#
最后
以上就是细心母鸡最近收集整理的关于linux shell sed中 使用 自定义变量 内置变量的全部内容,更多相关linux内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复