Bash 如何在Unix中进行索引到索引值的比较
我有一个场景,我使用array\u 1[index]值检查array\u 2[index]值 我下面的代码工作起来很有魅力,但是有什么不同的方法可以达到同样的效果吗 我正在寻找一种处理2个数组列表值并进行比较的独特方法, 只有几行代码 我的代码:Bash 如何在Unix中进行索引到索引值的比较,bash,shell,unix,awk,text-processing,Bash,Shell,Unix,Awk,Text Processing,我有一个场景,我使用array\u 1[index]值检查array\u 2[index]值 我下面的代码工作起来很有魅力,但是有什么不同的方法可以达到同样的效果吗 我正在寻找一种处理2个数组列表值并进行比较的独特方法, 只有几行代码 我的代码: #!/bin/bash array_1=(4 4 3) array_2=(4 1 3) i='' j='' count=0 for i in "${array_1[@]}" do i="cmd ${array_1
#!/bin/bash
array_1=(4 4 3)
array_2=(4 1 3)
i=''
j=''
count=0
for i in "${array_1[@]}"
do
i="cmd ${array_1[$count]}"
j="cmd ${array_2[$count]}"
if [ "$i" -eq "$j" ]
then
echo "${array_1[$count]} match with ${array_2[$count]}"
count=$(( count + 1 ))
else
echo "${array_1[$count]} does not match with ${array_2[$count]}"
exit 1
fi
done
注意:代码已通过检查,未发现错误
如果两个数组的值不同,则输出如下:
array_1=(4 4 3)
array_2=(4 1 3)
array_1=(4 4 3)
array_2=(4 4 3)
输出:
4 match with 4
4 does not match with 1
4 match with 4
4 match with 4
3 match with 3
如果两个数组具有相同的值,则输出如下:
array_1=(4 4 3)
array_2=(4 1 3)
array_1=(4 4 3)
array_2=(4 4 3)
输出:
4 match with 4
4 does not match with 1
4 match with 4
4 match with 4
3 match with 3
与您最近提出的其他shell问题一样,您根本不应该在shell中执行此操作,但由于您似乎专注于执行此操作,以下是如何简单地执行您正在执行的操作(减去确切的输出格式): 下面是如何在shell中编写问题中的代码(如果数组大小不同,现有代码将失败): 但更好的方法是,假设出于某种原因您确实需要从2个shell阵列开始,那么:
$ cat tst.sh
#!/usr/bin/env bash
array_1=(4 4 3)
array_2=(4 1 3)
awk -v string_1="${array_1[*]}" -v string_2="${array_2[*]}" '
BEGIN {
size1 = split(string_1,array_1)
size2 = split(string_2,array_2)
maxSize = (size1 > size2 ? size1 : size2)
for (i=1; i<=maxSize; i++) {
val1 = array_1[i]
val2 = array_2[i]
if ( val1 == val2 ) {
printf "%s match with %s\n", array_1[i], array_2[i]
}
else {
printf "%s does not match with %s\n", array_1[i], array_2[i]
exit 1
}
}
}
'
与您最近提出的其他shell问题一样,您根本不应该在shell中执行此操作,但由于您似乎专注于执行此操作,以下是如何简单地执行您正在执行的操作(减去确切的输出格式): 下面是如何在shell中编写问题中的代码(如果数组大小不同,现有代码将失败): 但更好的方法是,假设出于某种原因您确实需要从2个shell阵列开始,那么:
$ cat tst.sh
#!/usr/bin/env bash
array_1=(4 4 3)
array_2=(4 1 3)
awk -v string_1="${array_1[*]}" -v string_2="${array_2[*]}" '
BEGIN {
size1 = split(string_1,array_1)
size2 = split(string_2,array_2)
maxSize = (size1 > size2 ? size1 : size2)
for (i=1; i<=maxSize; i++) {
val1 = array_1[i]
val2 = array_2[i]
if ( val1 == val2 ) {
printf "%s match with %s\n", array_1[i], array_2[i]
}
else {
printf "%s does not match with %s\n", array_1[i], array_2[i]
exit 1
}
}
}
'
您在问题中添加了
awk
使用awk
您可以:
awk 'FNR==NR{x[FNR]=$1; next}
x[FNR]==$1{print x[FNR] " match with " $1; next}
{print x[FNR] " does not match with " $1}
' <(printf "%s\n" "${array_1[@]}") <(printf "%s\n" "${array_2[@]}")
打印(示例中使用两个数组):
如果要在第一次不匹配后退出,只需在打印后添加exit
:
awk 'FNR==NR{x[FNR]=$1; next}
x[FNR]==$1{print x[FNR] " match with " $1; next}
{print x[FNR] " does not match with " $1; exit 1}
' <(printf "%s\n" "${array_1[@]}") <(printf "%s\n" "${array_2[@]}")
建设:
cmdX <(cmdY)
一旦你将数组并排或者两个文件放在一起,然后将它们与
awk
diff
comm
perl
或者ruby
进行比较就很简单了。你在问题中添加了awk
使用awk
您可以:
awk 'FNR==NR{x[FNR]=$1; next}
x[FNR]==$1{print x[FNR] " match with " $1; next}
{print x[FNR] " does not match with " $1}
' <(printf "%s\n" "${array_1[@]}") <(printf "%s\n" "${array_2[@]}")
打印(示例中使用两个数组):
如果要在第一次不匹配后退出,只需在打印后添加exit
:
awk 'FNR==NR{x[FNR]=$1; next}
x[FNR]==$1{print x[FNR] " match with " $1; next}
{print x[FNR] " does not match with " $1; exit 1}
' <(printf "%s\n" "${array_1[@]}") <(printf "%s\n" "${array_2[@]}")
建设:
cmdX <(cmdY)
一旦你将数组并排或者两个文件放在一起,然后将它们与
awk
diff
comm
perl
或者ruby
进行比较就很简单了。你应该用你实际使用的shell来标记你的问题,而不仅仅是shell
,因为不同的shell有非常不同的处理方式做事情。为什么你有i=“cmd${array\u 1[$count]}”
而不是i=“${array\u 1[$count]}”
?你应该用你实际使用的shell来标记你的问题,而不仅仅是shell
,因为不同的shell有非常不同的做事方式。为什么你有i=“cmd${array\u 1[$count]}
而不是i=“${array\u 1[$count]}”
?
paste <(printf "%s\n" "${array_1[@]}") <(printf "%s\n" "${array_2[@]}")
4 4
4 1
3 3