简体   繁体   English

如何使用 bash 脚本在文件中的特定单词或行后添加一行?

[英]How can i add a block of line after a specific word or line in a file using bash script?

This is file content这是文件内容

  name: kubernetes
contexts:
- context:
    cluster: kubernetes
    user: kubernetes-admin
  name: kubernetes-admin@kubernetes
current-context: kubernetes-admin@kubernetes
kind: Config
preferences: {}
users:
- name: kubernetes-admin
  user:

i need to add the following block of code after the word contexts which is second line in above file content我需要在上面文件内容中的第二行的单词上下文之后添加以下代码块

- context:
       cluster: kubernetes
       namespace: myco-dev
       user: kubernetes-admin
     name: myco/DEV
   - context:
       cluster: kubernetes
        namespace: myco-ua
        user: kubernetes-admin
     name: myco/UA

so the final output should look like所以最终的输出应该是这样的

contexts:
- context:
    cluster: kubernetes
    user: kubernetes-admin
  name: kubernetes-admin@kubernetes
- context:
    cluster: kubernetes
    user: kubernetes-admin
    namespace: myco-dev
  name: myco/DEV
- context:
    cluster: kubernetes
    user: kubernetes-admin
    namespace: myco-ua
  name: myco/UA
current-context: kubernetes-admin@kubernetes
kind: Config
preferences: {}

I don't know YAML which is according to some comments that the data is from that type.我不知道 YAML,根据一些评论,数据来自该类型。 In the assumption of the data will come from files, let say file1 and file2.假设数据来自文件,比如说文件 1 和文件 2。 We can use ed.我们可以使用 ed。 (again I don't know YAML or where the data is coming from) (同样我不知道 YAML 或数据来自哪里)

Content of file1 is file1 的内容是

  name: kubernetes
contexts:
- context:
    cluster: kubernetes
    user: kubernetes-admin
  name: kubernetes-admin@kubernetes
current-context: kubernetes-admin@kubernetes
kind: Config
preferences: {}
users:
- name: kubernetes-admin
  user:

Content of file2 is file2 的内容是

- context:
       cluster: kubernetes
       namespace: myco-dev
       user: kubernetes-admin
     name: myco/DEV
   - context:
       cluster: kubernetes
        namespace: myco-ua
        user: kubernetes-admin
     name: myco/UA

To insert the content of file2 to file1 we do.将 file2 的内容插入到 file1 中。

 printf '%s\n' '2 r file2' w | ed -s file1

Checkout the content of file1检出file1的内容

cat file1

  name: kubernetes
contexts:
- context:
       cluster: kubernetes
       namespace: myco-dev
       user: kubernetes-admin
     name: myco/DEV
   - context:
       cluster: kubernetes
        namespace: myco-ua
        user: kubernetes-admin
     name: myco/UA
- context:
    cluster: kubernetes
    user: kubernetes-admin
  name: kubernetes-admin@kubernetes
current-context: kubernetes-admin@kubernetes
kind: Config
preferences: {}
users:
- name: kubernetes-admin
  user:

The number 2 is the address aka line number数字 2 是地址又名行号

The r afaik is read which means read file2 r afaik 被读取,这意味着读取 file2

The w means write. w 表示写入。

see

 man(1p) ed

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在bash中特定单词旁边的文本文件中添加一行? - How can I add a line into a text file next to specific word in bash? 如何替换bash脚本中文件的特定行? - How can I replace a specific line of a file in bash script? 我如何创建Linux脚本(bash,sed,awk…)在配置文件的特定部分添加一行 - How can i create a Linux Script (bash, sed, awk…) to add a line in a specific section of a configuration's file 如何使用Bash读取特定行和特定字段? - How can i read specific line and specific field using Bash? 如何在 bash 命令行或脚本中位置更改的文件中替换特定字符? - How can I replace a specific character in a file where it's position changes in bash command line or script? 在 Bash 中,如何在文件的每一行之后添加一个字符串? - In Bash, how do I add a string after each line in a file? 如何在 BASH 中指定行之后的每一行添加特定字符串 - How to add specific string to every line after the specifed line in BASH Bash:如何在文件中的特定位置插入一行? - Bash: How can I insert a line to a specific location within a file? 如何替换 unix 文件中特定行的单词 - How can I replace a word at a specific line in a file in unix Bash:如何在倒数第二个单词的特定行添加字符串 - Bash: How to add a string at a specific line of last second word
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM