gpt4 book ai didi

linux - 使用 Ansible 仅删除文件中第一次出现的行

转载 作者:太空宇宙 更新时间:2023-11-04 09:19:33 24 4
gpt4 key购买 nike

我正在使用 ansible v2.2.1,我只需要删除文件中第一次出现的一行。所以我的 ansible 剧本如下。

---   
- hosts: localhost
tasks:
- name: read file content
command: cat occurence.txt
register: fc
- debug: var=fc

- debug: msg="{{ item }}"
with_items:
- "{{ fc.stdout_lines }}"

- name: first occurence in file
shell: sed -i '0,/{{ item }}/{/{{ item }}/d;}' occurence.txt
with_items:
- "{{ fc.stdout_lines }}"
register: remove
- debug: var=remove

occurence.txt文件有以下内容

this is apple
this is apple
this is banana
this is apple
this is orange

如何只删除第一次出现的“this is apple”行并保留其余行?

最佳答案

我喜欢 Yunnosch 对这个问题的解释。另一种方法是:

sed '0,/^this is apple$/{//d}'

从文件开头到第一次出现 apple 行,只删除与前一个匹配项匹配的行。

关于linux - 使用 Ansible 仅删除文件中第一次出现的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43502772/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com