bash脚本中的SED Escape [英] SED Escape in bash script

查看:131
本文介绍了bash脚本中的SED Escape的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的tempfile.txt具有以下内容[典型的Cisco配置文件]

My tempfile.txt has below contents [Typical Cisco Configuration File]

interface GigabitEthernet1/11
 no ip address
 shutdown
 rmon collection stats 6010 owner monitor
!
interface GigabitEthernet1/12
 no ip address
 shutdown
 rmon collection stats 6011 owner monitor
!
interface GigabitEthernet1/13
 no ip address
 shutdown
 rmon collection stats 6012 owner monitor
!
interface GigabitEthernet1/14
 no ip address
 shutdown
 rmon collection stats 6013 owner monitor
!

我试图遍历每个块进行一些操作 例子

I am trying to loop through to do some operation on each block Example

interface GigabitEthernet1/14 --> start of interface block
     no ip address
     shutdown
     rmon collection stats 6013 owner monitor
    ! --> end of interface block

我还有另一个文件,其下面的数据为intefaces.txt

I have another file which has below data intefaces.txt

interface GigabitEthernet1/11
interface GigabitEthernet1/12
interface GigabitEthernet1/13
interface GigabitEthernet1/14

现在想在每个块中进行一些更改,这就是为什么我尝试通过以下方式循环访问块

Now want to do some changes in each block thats why I am trying to access block in loop via

while read line
do
sed -n '/$line/,/!/p' tempfile.txt # this should get me block
#code for doing some changes in block
done < interfaces.txt 

现在,因为我的$ line将是interfaces.txt的行之一,例如:interface GigabitEthernet1/11

Now since my $line will be one of line of interfaces.txt eg: interface GigabitEthernet1/11

如何像在循环&中所做的那样逃避/我的界面将始终包含/字符

how can I escape / as I am doing in loop & my interfaces will always contain / character

推荐答案

在搜索中告诉sed要在替换/的位置使用哪个字符,并在其前面加上反斜杠:

Tell sed which character to use in place of / in the search by prefixing it with a backslash:

sed -n "\@${line}@,/!/p" ...

这篇关于bash脚本中的SED Escape的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆