使用SED或尴尬提取行并将其保存在文件中

发布于 2025-01-19 13:44:38 字数 1455 浏览 4 评论 0原文

亲爱的 Stackoverflow 社区,

我正在尝试获取字符串或行的值或部分。

Kubernetes init 提供了 2 个 kubeadm join 命令。

我想提取第一个并将其保存在文件中,类似地提取第二个并将其保存在文件中。

以下是我尝试从文件中提取的文本:

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

目标 -

提取两个 kubeadm join 命令并将它们保存在不同的文件中以实现自动化。

到目前为止使用的命令 -

sed -ne '/--control-plane --certificate-key/p' token 使用上面的命令,如果可以的话,我想提取值并将其保存在文件中。

另一个命令 -

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

token 是文件名

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唯憾梦倾城 2025-01-26 13:44:38

您没有显示预期的输出,所以这有点猜测,但是:

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

根据您提供的输入,应该做我认为您想要的事情。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文