advice required to do a replace task based on input criteria
Centos.
I have a load of config files - cisco configs actually. I need to change the interface names per device. There are 30 configs for each device, however the changes I need to make are the same despite the differences in the configs. I though Ill try and get soem help scripting this ! So if I have R1, R2... etc I need to perform some replace text on a number of lines based on a set of rules that I define. I am not sure wether to use a script that has a load of sed and case statements? - not sure how to do case in bash, or should I try to learn perl for this task? E.g for an input file named R1 do this: case: input filename = R1 S1/0/1 replace with S1/0 FastEthernet 0/0 replace with Ethernet 0/0 case: input filename = R2 S1/0/1 replace with S1/2 GigabitEthernet 0/0 replace with Ethernet 0/0 etc.. |
Perhaps something like this in Bash:
Code:
for OUT in R[1-9]* ; do Code:
case "$OUT" in Code:
if [ "$OUT" = "R7" ] || [ "$OUT" = "R13" ]; then |
Hi thank you very much for the prompt reply - I will give this a try now. I'll write a set of psuedo rules and then convert them.
So what kind of variable is regex() ? I am terrible at bash scripting. |
Quote:
As an example, Code:
array=('first thing' foo 'bar' *.jpeg) Code:
array=() Code:
ls -1 'first thing' foo 'bar' *.jpeg |
# \bin\bash
for OUT in r[1-6]* ; do IN="$OUT.old" mv -f "$OUT" "$IN" || exit $? regex=() if [ "$OUT" = "r1.txt" ] || [ "$OUT" = "r2.txt" ];then regex=("${regex[@]}" -e "s|interface Serial 0\/0|interface Serial 1\/0|g") #r1.txt #interface Serial 0/0 --> interface Serial 1/0 #interface Serial 0/0.1 --> interface Serial 1/0.1 fi #r2.txt #interface Serial 0/0 --> interface Serial 1/0 #interface Serial 0/0.1 --> interface Serial 1/0.1 if [ "$OUT" = "r3.txt" ];then regex=("${regex[@]}" -e "s|interface FastEthernet0\/0|interface Ethernet0\/0|g") regex=("${regex[@]}" -e "s|interface FastEthernet0\/1|interface Ethernet0\/1|g") fi #r3.txt #interface FastEthernet0/0 --> interface Ethernet0/0 #interface FastEthernet0/1 --> interface Ethernet0/1 if [ "$OUT" = "r4.txt" ] || [ "$OUT" = "r5.txt" ];then regex=("${regex[@]}" -e "s|interface FastEthernet0\/0|interface Ethernet0\/0|g") regex=("${regex[@]}" -e "s|interface FastEthernet0\/1|interface Ethernet0\/1|g") regex=("${regex[@]}" -e "s|interface Serial 0\/0\/0|interface Serial 1\/0|g") fi #r4.txt #interface FastEthernet0/0 --> interface Ethernet0/0 #interface FastEthernet0/1 --> interface Ethernet0/1 #interface Serial0/0/0 --> interface Serial1/0 #interface Serial0/0/0.1 point-to-point --> interface Serial1/0.1 point-to-point #r5.txt #interface FastEthernet0/0 --> interface Ethernet0/0 #interface FastEthernet0/1 --> interface Ethernet0/1 #interface Serial0/0/0 --> interface Serial1/0 #interface Serial0/1/0 --> interface Serial1/1 if [ "$OUT" = "r6.txt" ] ;then regex=("${regex[@]}" -e "s|interface FastEthernet0\/0|interface Ethernet0\/0|g") regex=("${regex[@]}" -e "s|interface Serial 0\/0\/0|interface Serial 1\/0|g") fi #r6.txt #interface Serial0/0/0 --> interface Serial1/0 #interface FastEthernet0/0 --> interface Ethernet0/0 #interface FastEthernet0/0.67 --> interface Ethernet0/0.67 #interface FastEthernet0/0.146 --> interface Ethernet0/0.146 echo $IN echo $OUT # Apply. sed "${regex[@]}" "$IN" > "$OUT" || exit $? chown --reference="$IN" "$OUT" &>/dev/null chmod --reference="$IN" "$OUT" &>/dev/null done This is what I have so far and the changes I need to make in the comments. I figure that changing the subinterfaces will be covered by the global replace, e.g. 0/0 -> 1/0 and 0/0.1 -> 1/0.1 as the 0/0 and the 1/0 are the beginnings of the other. Was the escaping correct for the forward slashes? also what is the $? character? There is 20 different configs for each router, but the interface changes are always the same, so it would be great to bea able to send them through the script before pasting them onto the routers. |
Ok awesome that worked now the last piece of the puzzle. Sed is just a monster to understand
I am trying to add a new line after every occurence of the "interface Ethernet0/*" the text in the new line sets the interfaces to full duplex. so interface Ethernet0/0 would become: interface Ethernet0/0 Duplex Full as would interface Ethernet0/1 become interface Ethernet0/1 duplex full so far I have a mash up: sed -e |interface Ethernet 0\/0.*|/a\Duplex Full|g r7.txt any ideas on the syntax of this one? |
Please use [CODE][/CODE] tags around code and exact strings. It keeps indentation intact, and makes it much easier to read.
Quote:
These two sed commands do the exact same thing: Code:
s|foo/bar|baz|g Quote:
This also means that Code:
some-command... Additionally, Code:
some-command... Quote:
Code:
-e '/^[\t\v\f\r ]*interface[\t\v\f\r ]/ a\\Duplex full' Note that you seem to be having some issues with proper quoting. I recommend you take a look at the Quoting chapter in the Bash Reference Manual; the first three subsections (Escape Character, Single Quotes, and Double Quotes) are the most important. Most of the problems I've seen in shell scripts stem for incorrect/bad quoting. You really should learn it first, and apply it always. I recommend "defensive quoting", i.e. using the strictest form of quoting that will work, even when quoting might not be technically required, and it has served me extremely well. The most basic rules are actually quite simple:
|
I only need full duplex on 10meg ethernet interfaces, as 100 meg fast enthernet auto detects the correct speed. This is the one I got working in the end. It must look like spaghetti!
Code:
Thank you for all of your help. |
All times are GMT -5. The time now is 11:39 AM. |