![](/img/trans.png)
[英]How to extract a specific field from output of tasklist on the windows command line
[英]Extract a specific column from the windows command prompt output using Ansible
我想获得第3和第5列输出。 我在我的剧本中使用了以下命令。
win_shell: dir | awk '{print $3,$5}'
但我输出如下。
"awk : The term 'awk' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the ",
"spelling of the name, or if a path was included, verify that the path is correct and try again.",]
当我通过Windows提示符执行命令时,我得到了预期的输出。 请帮忙。
你可以在Ansible中执行列提取而不是使用awk
。 例如,假设您已在名为result
的变量中注册了win_shell
任务的输出,则可以执行以下操作:
- debug:
var: item.split()|json_query('[[2], [4]]')
loop: "{{ output.stdout_lines }}"
这将显示每行输出的第3和第5列(数组为零索引)。 也许你想把它放在一个列表而不是只显示它:
- set_fact:
data: "{{ data|default([]) + [item.split()|json_query('[[2], [4]]')] }}"
loop: "{{ output.stdout_lines }}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.