arrays - parsing two multi-line grep results to a single bash asociative array
问题描述
I'm retrieving filtered text from a group of emails by using these two commands:
1)
curl -u$username:$password --silent "https://mail.google.com/mail/feed/atom" | grep -oPm1 "(?<=<summary>The host is: )(\S+)+" | sed '1d'
2)
curl -u$username:$password --silent "https://mail.google.com/mail/feed/atom" | grep -oPm1 "(?<=the serial is\s)(\w+)+" | sed '1d'
each one returns:
1)
xy36
xy34
xy32
xy30
xy29
xy28
xy26
2)
Xooz4woo
Oyaith4k
AiN7fie4
ongae6Ro
phoh6fiR
Ohfoh0eM
and i need that data to be parsed to an associative array in bash, in a way i can do a loop write/read it, and use each entry on my code. The array should be:
machines{
{xy36,Xooz4woo}
{xy34 Oyaith4k}
{xy32, AiN7fie4}
{xy30, ongae6Ro}
{xy29, phoh6fiR}
{xy28, Ohfoh0eM}}
so i can access it like:
echo "${machines[xy36]}"
result: Xooz4woo
how can i do that???
解决方案
Put the result of each in an array, then loop over the array indexes.
declare -A machines
hosts=($(first command))
serials=($(second command))
for ((i=0; i < ${#hosts[@]}; i++))
do
machines[${hosts[$i]}]=${serials[$i]}
done
推荐阅读
- javascript - npm 从 package-lock.json 文件中创建一个 package.json 文件?
- javascript - 如何在对象中迭代和格式化我的值,同时将其作为对象返回?
- bash - 从脚本中获取环境变量并传递给 Dockerfile
- postgresql - Sailsjs v1 PostgreSQL 多对多关联 addToCollection 由于模型验证而失败
- neo4j - 使用 C# 驱动程序从 Neo4j 查询中读取结果
- awk - awk 打印带有标准的行 - 大于
- sql-server - 我想查找每个帐户在某个日期之前的最后一笔交易
- python-3.x - 根据它们之间的距离对线进行分组
- node.js - 使用 fetch 响应身份验证
- python - Python ValueError:无法将序列复制到数组