我正在使用 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/