检查变量是否匹配,如果找不到差异

发布于 2025-01-18 07:11:43 字数 640 浏览 1 评论 0原文

我有2个变量的数据。

var1='abc;mno;def'
var2='mno;xyz'
** var2 can also be 'def;mno;abc'   

我想比较var1和var2

if [[ $var1=$var2 ]]
  then  
    echo "MATCHED"
  else 
    echo "Not Matched"
fi

这将为我提供基本验证,但我的要求有些不同。我想要var1中存在的数据,但在var2中缺少数据,也存在var2中存在的数据,但在var1中缺少我,

我希望以下格式的数据:

abc;mno;def,mno;xyz,Not Matched,abc;def(## data present in var1 but missing in var2),xyz(data present in var2 but missing in var1)

第二种情况 var1 ='abc; mno; def' var2 ='def; mno; abc'

输出

abc;mno;def,def;mno;abc,Matched, ,

任何建议都很棒。

I have data in 2 variables.

var1='abc;mno;def'
var2='mno;xyz'
** var2 can also be 'def;mno;abc'   

I want to compare var1 and var2

if [[ $var1=$var2 ]]
  then  
    echo "MATCHED"
  else 
    echo "Not Matched"
fi

This would give me basic validation but my requirement is bit different. I want data present in var1 but missing in var2 and also data present in var2 but missing in var1

I want data in below format :

abc;mno;def,mno;xyz,Not Matched,abc;def(## data present in var1 but missing in var2),xyz(data present in var2 but missing in var1)

Second case
var1='abc;mno;def'
var2='def;mno;abc'

Output

abc;mno;def,def;mno;abc,Matched, ,

Any suggestion would be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

胡大本事 2025-01-25 07:11:43

假设您使用的是 bash,请尝试以下操作:

#!/bin/bash

var1='abc;mno;def'
var2='mno;xyz'
#var2='def;mno;abc'

while IFS=';' read -r c1 c2 c3; do
    [[ -n $c1 ]] && col1+=("$c1")       # present in var1 only
    [[ -n $c2 ]] && col2+=("$c2")       # present in var2 only
    [[ -n $c3 ]] && col3+=("$c3")       # present in both var1 and var2
done < <(comm <(tr ';' '\n' <<< "$var1" | sort) <(tr ";" "\n" <<< "$var2" | sort
) | tr '\t' ';')

if (( ${#col1[@]} == 0 && ${#col2[@]} == 0 )); then
    msg="Matched"                       # both col1 and col2 are empty
else
    msg="Not Matched"
fi

printf "%s,%s,%s,%s,%s\n" "$var1" "$var2" "$msg" \
        "$(IFS=';'; echo "${col1[*]}")" "$(IFS=';'; echo "${col2[*]}")"

输出:

abc;mno;def,mno;xyz,Not Matched,abc;def,xyz

第二种情况的输出:

abc;mno;def,def;mno;abc,Matched,,
  • tr ';' '\n' <<< “$var1” | sort$var1 分成已排序的行
    要提供给 comm 的数据。与 $var2 相同。
  • comm <(...) <(...) 比较两个输入,然后将数据排序为
    三列取决于数据的唯一性。
  • 制表符(comm 输出的字段分隔符)被替换
    ; 一起使用 read 命令进行正确处理。否则读取
    将跳过前导字段分隔符。
  • 第一列中的数据(var1 独有)累积在
    数组col1。与 col2col3 相同。
  • 我们可以通过检查长度来检查 $var1$var2 是否匹配
    数组 col1col2。如果两者都为空,则变量匹配。

这是一个 awk 替代方案,仅供参考:

#!/bin/bash

var1='abc;mno;def'
var2='mno;xyz'
#var2='def;mno;abc'

awk -v var1="$var1" -v var2="$var2" '
BEGIN {
    split(var1, ai, ";")        # split var1 on ";"
    split(var2, bi, ";")        # same as above
    for (i in ai) a[ai[i]]      # generate associavive array
    for (i in bi) b[bi[i]]      # same as above

    for (i in a) {
        if (! (i in b)) {       # seen in a, not b
            uniq1[i]            # then store it in uniq1
        }
    }
    for (i in b) {
        if (! (i in a)) {       # seen in b, not a
            uniq2[i]            # then store it in uniq2
        }
    }

    fs = ""
    for (i in uniq1) {          # join elements of uniq1 with ";"
        u1 = u1 fs i            # into a string u1
        fs = ";"
    }
    fs = ""
    for (i in uniq2) {          # join elements of uniq2 with ";"
        u2 = u2 fs i            # into a string u2
        fs = ";"
    }

    msg = (length(u1) == 0 && length(u2) == 0)? "Matched" : "Not Matched"
    printf("%s,%s,%s,%s,%s\n", var1, var2, msg, u1, u2)
}'

Assuming you are using bash, would you please try the following:

#!/bin/bash

var1='abc;mno;def'
var2='mno;xyz'
#var2='def;mno;abc'

while IFS=';' read -r c1 c2 c3; do
    [[ -n $c1 ]] && col1+=("$c1")       # present in var1 only
    [[ -n $c2 ]] && col2+=("$c2")       # present in var2 only
    [[ -n $c3 ]] && col3+=("$c3")       # present in both var1 and var2
done < <(comm <(tr ';' '\n' <<< "$var1" | sort) <(tr ";" "\n" <<< "$var2" | sort
) | tr '\t' ';')

if (( ${#col1[@]} == 0 && ${#col2[@]} == 0 )); then
    msg="Matched"                       # both col1 and col2 are empty
else
    msg="Not Matched"
fi

printf "%s,%s,%s,%s,%s\n" "$var1" "$var2" "$msg" \
        "$(IFS=';'; echo "${col1[*]}")" "$(IFS=';'; echo "${col2[*]}")"

Output:

abc;mno;def,mno;xyz,Not Matched,abc;def,xyz

Output for the second case:

abc;mno;def,def;mno;abc,Matched,,
  • tr ';' '\n' <<< "$var1" | sort breaks $var1 into lines of sorted
    data to feed to comm. Same with $var2.
  • comm <(...) <(...) compares the two inputs then sorts the data into
    three columns depending on the uniqueness of the data.
  • The tab characters, the field separator of comm output, are replaced
    with ; to be properly handled with read command. Otherwise read
    will skip leading field separators.
  • The data in the 1st column (unique to var1) are accumulated in the
    array col1. Same with col2 and col3.
  • We can check for a match of $var1 and $var2 by examining the length
    of arrays col1 and col2. If both are empty, the variables match.

Here is an awk alternative just for reference:

#!/bin/bash

var1='abc;mno;def'
var2='mno;xyz'
#var2='def;mno;abc'

awk -v var1="$var1" -v var2="$var2" '
BEGIN {
    split(var1, ai, ";")        # split var1 on ";"
    split(var2, bi, ";")        # same as above
    for (i in ai) a[ai[i]]      # generate associavive array
    for (i in bi) b[bi[i]]      # same as above

    for (i in a) {
        if (! (i in b)) {       # seen in a, not b
            uniq1[i]            # then store it in uniq1
        }
    }
    for (i in b) {
        if (! (i in a)) {       # seen in b, not a
            uniq2[i]            # then store it in uniq2
        }
    }

    fs = ""
    for (i in uniq1) {          # join elements of uniq1 with ";"
        u1 = u1 fs i            # into a string u1
        fs = ";"
    }
    fs = ""
    for (i in uniq2) {          # join elements of uniq2 with ";"
        u2 = u2 fs i            # into a string u2
        fs = ";"
    }

    msg = (length(u1) == 0 && length(u2) == 0)? "Matched" : "Not Matched"
    printf("%s,%s,%s,%s,%s\n", var1, var2, msg, u1, u2)
}'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文