如何使用Shell或Ansible从文本文件内容中获取PID?

问题描述 投票:0回答:1

如何获取PID的值?并将格式改为:

pid: 12032
pid: 11948 

文件内容:

# cat /tmp/log.txt 
["", "whoami", "Copyright2024", "", "", "git.exe            pid: 12032  type: File           164: E:\\tmp\\", "git.exe            pid: 11948  type: File           12C: E:\\tmp\\"]
python-3.x shell ansible
1个回答
0
投票

您可以使用以下代码从给定文本中提取 pid

---
- name: To get PID from log.txt
  hosts: 127.0.0.1
  tasks:
    - name: Extract  PID
      shell: "grep -o 'pid: [0-9]\\+' /tmp/log.txt > /tmp/pid.txt"
© www.soinside.com 2019 - 2024. All rights reserved.