Arrays bash——将特定URL从字符串隔离到数组中

Arrays bash——将特定URL从字符串隔离到数组中,arrays,string,bash,loops,grep,Arrays,String,Bash,Loops,Grep,我对bash脚本相当有信心,但这似乎有点让我不知所措 我想做的是拿一根绳子 page_content=<div class="contact_info_wrap"><img src="http://example.com/UserMedia/gafgallery/icons/email_icon.png" style="border-width: 0px; border-style: solid;" width="40" /><img alt="" src="exa

我对bash脚本相当有信心,但这似乎有点让我不知所措

我想做的是拿一根绳子

page_content=<div class="contact_info_wrap"><img src="http://example.com/UserMedia/gafgallery/icons/email_icon.png" style="border-width: 0px; border-style: solid;" width="40" /><img alt="" src="example.com/UserMedia/gafgallery/icons/loc_icon.png" style="border-width: 0px; border-style: solid;" width="40" />
在then中,我试图获取$page_内容中的每个URL,仅包含http://example.com,并将它们添加到数组中。虽然我真的不知道从哪里开始!我想以这样的方式结束:

This[0]='http://example.com/the/first/url/containing/example.com'
This[1]='http://example.com/the/second/url/containing/example.com'
This[2]='etc ... '
This[3]='etc ... '

有没有一种简单有效的方法来完成这项工作?

试试这样的方法:

#!/bin/bash
sql_request()
{
mysql --login-path=myhostalias -Dywpadmin_current_content -e"SELECT page_id, page_content FROM client_content WHERE client_section_id = '$client_section_id'"
}

filter_urls()
{
grep -E -o "(href|src)=\"[^\"]*$1[^\"]*" | cut -d'"' -f2 | sort -u
}

declare -a array=()
while read page_id page_content
do
  while read url
  do
     array+=("$url")
  done < <(filter_urls "example.com" <<<"$page_content")
done < <(sql_request)

printf "%s\n" "${array[@]-}" # Just to show array content
我不是mysql的专家,我只是复制/粘贴了你的commanda,假设它能工作。我假设您需要一个包含所有读取页面URL的数组,但如果您正在寻找其他内容,则可以轻松调整解决方案

此外,我假设在不更改IFS或使用common-r选项的情况下,通过read正确读取数据,但这是您可能想要做的事情

一些关注点:


注意进程替换的使用<您能否提供完整的代码,包括读取与每个URL关联的内容的部分?没有太多要显示的内容-字面意思:mysql-login path=myhostalias-Dywpadmin\u current\u content-eSELECT page\u id,来自客户端内容的页面内容,其中客户端部分id=“$client\u section\u id'”;同时读取页面id页面内容;做-然后你看到上面的代码你是正确的报价。。。你的答案看起来很有用。。。我明天会测试它,如果它有效,我会接受你的答案,否则我会继续这个对话。。谢谢你的回答!稍微调整一下,这正是我需要的。。非常感谢。
#!/bin/bash
sql_request()
{
mysql --login-path=myhostalias -Dywpadmin_current_content -e"SELECT page_id, page_content FROM client_content WHERE client_section_id = '$client_section_id'"
}

filter_urls()
{
grep -E -o "(href|src)=\"[^\"]*$1[^\"]*" | cut -d'"' -f2 | sort -u
}

declare -a array=()
while read page_id page_content
do
  while read url
  do
     array+=("$url")
  done < <(filter_urls "example.com" <<<"$page_content")
done < <(sql_request)

printf "%s\n" "${array[@]-}" # Just to show array content