Ankush Panday
Ankush Panday

Reputation: 83

Extract the lines using sed or awk and save them in file

Dear Stackoverflow Community,

I am trying to grab the value or the part of the string or lines.

The Kubernetes init gives 2 kubeadm join commands.

I want to extract the first one and save it in a file and similarly extract the 2nd one and save it in a file.

Below is the text that I am trying to extract from the file:

You can now join any number of the control-plane node running the following command on each as root:

  kubeadm join 10.0.0.0:6443 --token jh88qi.uch1l58ri160bve1 \
    --discovery-token-ca-cert-hash sha256:f9c9ab441d913fec7d157c20f1c5e93c496123456ac4ec14ca8e02ab7f916d7fb \
    --control-plane --certificate-key 179e288571e33d3d68f5691b6d8e7cefa4657550fc0886856a52e2431hjkl7155

Please note that the certificate-key gives access to cluster sensitive data, keep it secret!
As a safeguard, uploaded-certs will be deleted in two hours; If necessary, you can use
"kubeadm init phase upload-certs --upload-certs" to reload certs afterward.

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 10.0.0.0:6443 --token jh88qi.uch1l58ri160bve1 \
    --discovery-token-ca-cert-hash sha256:f9c9ab441d913fec7d157c20f1c5e93c496123456ac4ec14ca8e02ab7f916d7fb

Goal -

Extract both kubeadm join commands and save them in different files for automation.

Commands Used till now -

sed -ne '/--control-plane --certificate-key/p' token With the above command, I want to extract value if I can and save it in a file.

The other command -

awk '/kubeadm join/{x=NR+2}(NR<=x){print}' token

token is the filename

Upvotes: 0

Views: 193

Answers (1)

Ed Morton
Ed Morton

Reputation: 204259

You didn't show the expected output so it's a bit of a guess but this:

awk -v RS= '/^ *kubeadm join/{print > ("out"NR); close("out"NR)}' file

should do what I think you want given the input you provided.

Upvotes: 1

Related Questions