Ciscoman's notes (Записки цыщика c дипломом)

I'm Cisco Champion Community member for 2017!

I'm Cisco Champion Community member for 2017!
"Cisco Champions are passionate about Cisco and happy to share our knowledge, experience, and feedback."

пятница, 11 февраля 2011 г.

How to conditionally replace two trings with sed

Yesterday I spotted trouble while editing my cisco text configs with large bash script.
 Let's assume we have part of configuration like this:
!
interface FastEthernet0/11
description BB1 E0/0
switchport access vlan 100
switchport mode dynamic desirable
!
interface FastEthernet0/12
switchport mode dynamic desirable
!
If you need to replace "switchport mode dynamic desirable" with "switchport mode trunk", you need first delete string  "switchport mode dynamic desirable" if preceding string is "switchport access vlan 100". In other case your configuration will not work appropriately. You can use vim text editor's command:

:%s/\( switchport access vlan 100\)\n switchport mode dynamic desirable/\1/
But I was unable to do something like this via sed or perl.
It was necessary to ask the opennet.ru community. Thanks to user with nickname temny. He provided this solution:


sed '/switchport access vlan/{N;s!\nswitchport mode dynamic desirable!!;}' file

Комментариев нет:

Отправить комментарий

Постоянные читатели

Поиск по этому блогу