Hello,
I am trying to find a way to replace a set of sequential numbers in a file with a different sequence using sed. This might be done easier using awk or some sort of bash script, but it seems to me there must be a way to do this easily with sed. Basically, what I am editing is a Cisco switch config. I want to change the sequence of ports to a different numbered sequence. Here is an example of what I am trying to do.
I want to change for example, the file:
Code:
cat testfile
interface FastEthernet0/1
speed 10
switchport access vlan 10
spanning-tree portfast
!
interface FastEthernet0/2
speed 10
switchport access vlan 10
spanning-tree portfast
!
interface FastEthernet0/3
speed 100
switchport access vlan 11
spanning-tree portfast
!
interface FastEthernet0/4
speed 10
switchport access vlan 11
spanning-tree portfast
To:
Code:
interface FastEthernet0/5
speed 10
switchport access vlan 10
spanning-tree portfast
!
interface FastEthernet0/6
speed 10
switchport access vlan 10
spanning-tree portfast
!
interface FastEthernet0/7
speed 100
switchport access vlan 11
spanning-tree portfast
!
interface FastEthernet0/8
speed 10
switchport access vlan 11
spanning-tree portfast
All that is being changed is the line "interface FastEthernet0/X" where X is the sequential value I want to change from 1-4 to 5-8.
I am fairly new to sed and linux in general, but I expected the replacement field in sed to also be able to handle a regular expression. However this command did not work:
cat testfile | sed 's/\/[1-4]/\/[5-8]/g'
This just replaces the FastEthernet0/X lines all with FastEthernet0/[5-8]. I have also tried the y/ switch in sed, which works for this example, but not for the full file I am trying to edit:
cat testfile | sed '/0\/[1-4]$/ y/[1234]/[5678]/'
This works, but I really want to replace the range 1-22 with 26-48, so this would be a really long sed statement:
cat testfile | sed '/0\/[1-22]$/ y/[123456789,10,11...]/[26,27,28,...]/'
Plus I have yet to figure out how to make y/ work with double digit entries.... so the above is probably a horrible example of y/.
Any help would really be appreciated, even if it is suggestions for awk or some sort of script.
Regards,
thefiend