python-golang-p1

This week I had to update several spreadsheets…. again donkey job, again try to work smarter. So there was a pattern, and I knew I could copy/paste to the spreadsheet all changes (192 changes per file…). So decided to create a python script to generate the output I needed. It was just new hostnames for devices. So it wasnt really difficult. Although I had to search for basic stuff like how to make a for loop in python. That’s what happens when yo dont use thing often.

Anyway, I managed to get my basic python script. And I could copy paste the output to the spreadsheet just fine.

$ cat rename.py 
for x in range(137,141):
    print("router-p1-r" + str(x) + "01")
    print("router-p1-r" + str(x) + "02")
    print("router-p1-r" + str(x) + "03")
    print()
$ 
$ python rename.py 
router-p1-r13701
router-p1-r13702
router-p1-r13703

router-p1-r13801
router-p1-r13802
router-p1-r13803

router-p1-r13901
router-p1-r13902
router-p1-r13903

router-p1-r14001
router-p1-r14002
router-p1-r14003

But now, I have been trying to learn golang (even though I dont master python neither bash….) and I thought this was a basic stuff to try in golang too. So again, I had to search for basic stuff. How to create a range: link. How to concatenate a string and a integer: link.

So managed to get this and looks like it does the same job:

$ cat rename.go 
package main

import "fmt"

func main() {
	for i := 137; i <= 141; i++ {
		fmt.Println(fmt.Sprint("router-p1-r", i, "01"))
		fmt.Println(fmt.Sprint("router-p1-r", i, "02"))
		fmt.Println(fmt.Sprint("router-p1-r", i, "03"))
		fmt.Println()
	}
}
$ 
$ go run rename.go 
router-p1-r13701
router-p1-r13702
router-p1-r13703

router-p1-r13801
router-p1-r13802
router-p1-r13803

router-p1-r13901
router-p1-r13902
router-p1-r13903

router-p1-r14001
router-p1-r14002
router-p1-r14003

router-p1-r14101
router-p1-r14102
router-p1-r14103

So got the same result in both languages. Keep going!

sed

This week I have to update a couple of yaml files and add a line in too many places. This is the chance to work smarter. So I searched how to add a line after a match and I found this link. So let’s use sed:


$ cat test.yaml 
bgp:
  bgp_as: 65000
  enable: true
  maximum_routes: 12000
  neighbors:
  - description: R1
    route_map_in: RM-IN
  - description: R2
    route_map_in: RM-IN
$ 
$ sed '/route_map_in: RM-IN/a route_map_out: RM-OUT' test.yaml
bgp:
  bgp_as: 65000
  enable: true
  maximum_routes: 12000
  neighbors:
  - description: R1
    route_map_in: RM-IN
route_map_out: RM-OUT
  - description: R2
    route_map_in: RM-IN
route_map_out: RM-OUT

But the I needed to add some spaces to be aligned properly… So I searched again and found this. Now try again adding the spaces we need using “\ ” for each one.

go:1.14.6|py:3.7.3|tomas@athens:~$ sed '/route_map_in: RM-IN/a \ \ \ \ route_map_out: RM-OUT' test.yaml
bgp:
  bgp_as: 65000
  enable: true
  maximum_routes: 12000
  neighbors:
  - description: R1
    route_map_in: RM-IN
    route_map_out: RM-OUT
  - description: R2
    route_map_in: RM-IN
    route_map_out: RM-OUT

So now redirect to a new file to fully check and then you can rename:

$ sed '/route_map_in: RM-IN/a \ \ \ \ route_map_out: RM-OUT' test.yaml > test.yaml.new
$ cat test.yaml.new 
$ mv test.yaml.new test.yaml

Now it is perfect aligned. It is kernel hack, but I am happy enough and saved a lot of time copy/paste and errors for sure.

IronFit

I finished this book this week. I was interested how you can prepare for an IronMan or a normal triathlon. I like to have a training plan for different levels of commitment. And the planning for the race itself. I have learned some tips to improve in the three sports. At some point I would like to try a triathlon (I need to join a swim gym and get a heart meter). My only concern for a bigger challenge is the left knee. But, step by step.