检查一个字符串数组与另一个数组 – Bash

huangapple go评论78阅读模式
英文:

Check an array of strings against another array - Bash

问题

我有两个列表。

我需要检查是否存在一个包含类似于aa或ab等子字符串的tar.gz文件。
我正在使用这个小函数:
https://stackoverflow.com/questions/2829613/how-do-you-tell-if-a-string-contains-another-string-in-posix-sh

local=(aa ab ac ad ae)
remote=(ab.tar.gz ac.tar.gz ad.tar.gz ae.tar.gz af.tar.gz)

contains() { test -n "$1" || test -z "$2" && test -z "${1##*"$2"*}"; }
for le in "${local[@]}"; do
    for re in "${remote[@]}"; do
        contains "$re" "$le" && to_dl+=("$re")
    done
done

这对我来说运行良好,但我需要一个第二个列表,其中包含本地列表中没有在远程列表中找到匹配项的元素。

英文:

I have two lists.

I have to check if there is a tar.gz which name contains a substring like aa or ab and so on.
I'm using this little function:
https://stackoverflow.com/questions/2829613/how-do-you-tell-if-a-string-contains-another-string-in-posix-sh

local=(aa ab ac ad ae)
remote=(ab.tar.gz ac.tar.gz ad.tar.gz ae.tar.gz af.tar.gz)

contains() { test -n "$1" || test -z "$2" && test -z "${1##*"$2"*}"; }
for le in "${local[@]}"; do
    for re in "${remote[@]}"; do
        contains "$re" "$le" && to_dl+=("$re")
    done
done

This works well for me, but I need a second list, which contains the elements of the local list, that don't have a match in the remote list.

答案1

得分: 0

Consider just grep -f.

$ tmp=$(printf "%s\n" "${remote[@]}" | grep -Ff <(printf "%s\n" "${local[@]}"))
$ readarray -t to_dl <<<"$tmp"
$ declare -p to_dl
declare -a to_dl=([0]="ab.tar.gz" [1]="ac.tar.gz" [2]="ad.tar.gz" [3]="ae.tar.gz")

I need a second list, which contains the elements of the local list, that don't have a match in the remote list.

Use grep -v.

In your code, just use if. Overall, prefer if over any &&.

if contains "$re" "$le"; then
    to_dl+=("$re")
else
    secondlist+=("$re")
fi
英文:

Consider just grep -f.

$ tmp=$(printf &quot;%s\n&quot; &quot;${remote[@]}&quot; | grep -Ff &lt;(printf &quot;%s\n&quot; &quot;${local[@]}&quot;))
$ readarray -t to_dl &lt;&lt;&lt;&quot;$tmp&quot;
$ declare -p to_dl
declare -a to_dl=([0]=&quot;ab.tar.gz&quot; [1]=&quot;ac.tar.gz&quot; [2]=&quot;ad.tar.gz&quot; [3]=&quot;ae.tar.gz&quot;)

> I need a second list, which contains the elements of the local list, that don't have a match in the remote list.

Use grep -v.

In your code, just use if. Overall, prefer if over any &amp;&amp;.

if contains &quot;$re&quot; &quot;$le&quot;; then
    to_dl+=(&quot;$re&quot;)
else
    secondlist+=(&quot;$re&quot;)
fi

答案2

得分: 0

Sure, here is the translated code:

小修改 OP 当前的代码:

local=(aa ab ac ad ae)
remote=(ab.tar.gz ac.tar.gz ad.tar.gz ae.tar.gz af.tar.gz)

unset to_dl not_there           # 或者:to_dl=() not_there=()

contains() { test -n "$1" || test -z "$2" && test -z "${1##*$2*}"; }

for le in "${local[@]}"
do
    for re in "${remote[@]}"
    do
        contains "$re" "$le" && { to_dl+=("$re"); continue 2; }
    done
    not_there+=("$le")
done

其中:
- `continue 2` - 如果找到匹配项,则跳到列表中的下一个子字符串 (`le`)- `not_there+=("$le")` - 如果内部循环未找到匹配项,则将子字符串保存在不同的数组中。

结果:

$ typeset -p to_dl not_there
declare -a to_dl=("ab.tar.gz" "ac.tar.gz" "ad.tar.gz" "ae.tar.gz")
declare -a not_there=("aa")
英文:

Small modification to OP's current code:

local=(aa ab ac ad ae)
remote=(ab.tar.gz ac.tar.gz ad.tar.gz ae.tar.gz af.tar.gz)

unset to_dl not_there           # or: to_dl=() not_there=()

contains() { test -n &quot;$1&quot; || test -z &quot;$2&quot; &amp;&amp; test -z &quot;${1##*&quot;$2&quot;*}&quot;; }

for le in &quot;${local[@]}&quot;
do
    for re in &quot;${remote[@]}&quot;
    do
        contains &quot;$re&quot; &quot;$le&quot; &amp;&amp; { to_dl+=(&quot;$re&quot;); continue 2; }
    done
    not_there+=(&quot;$le&quot;)
done

Where:

  • continue 2 - if we find a match then skip to the next substring (le) in the list
  • not_there+=(&quot;$le&quot;) - if inner loop finds no matches then save the substring in a different array

Results:

$ typeset -p to_dl not_there
declare -a to_dl=([0]=&quot;ab.tar.gz&quot; [1]=&quot;ac.tar.gz&quot; [2]=&quot;ad.tar.gz&quot; [3]=&quot;ae.tar.gz&quot;)
declare -a not_there=([0]=&quot;aa&quot;)

huangapple
  • 本文由 发表于 2023年6月12日 15:51:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76454567.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定