seeking assistance on comparing two arrays using bash:
array1=(disk1, disk2, disk3, disk5, disk7, vol1, vol2, vol3, vol4, vol5)
array2=(disk2, disk5 vol2, vol4 )
1) if two arrays have same elements; EXIT
else populate array3 & array4 with elements that are different between array1 & array2 as:
array3=(vol1, vol3, vol5)
array4=(disk1, disk3, disk7)
thanks in advance
RudiC
January 15, 2013, 10:42am
2
Try this and assign to arrays 3 & 4 accordingly:
$ grep -vf <(echo "${array2
[*]}"|tr -s ', ' '\n') <(echo "${array1
[*]}"|tr -s ', ' '\n')
disk1
disk3
disk7
vol1
vol3
vol5
looks like there is no "f" flag that goes along with grep (Solaris).Without the f flag, dont think i got what i wanted:
#!/usr/bin/bash
array1=( `cat /tmp/array1 `)
array2=( `cat /tmp/array2 `)
echo "Elements in array1: ${array1[@]}"
echo "Elements in array2: ${array2[@]}"
grep -v <(echo "${array2[*]}"|tr -s ', ' '\n') <(echo "${array1[*]}"|tr -s ', ' '\n')
./test-array.sh
Elements in array1: disk1 disk2 disk3 disk5 disk7 vol1 vol2 vol3 vol4 vol5
Elements in array2: disk2 disk5 vol2 vol4
disk1 disk2 disk3 disk5 disk7 vol1 vol2 vol3 vol4 vol5
[/CODE]
so the output is exactly array1 not the non-commom elements between the two.
I need: disk1 disk3 disk7 vol1 vol3 vol5
also i need (disk1 disk3 disk7) & (vol1 vol3 vol5) as two separate arrays from the output.
RudiC
January 16, 2013, 3:58am
4
From the man pages in this forum:
/usr/xpg4/bin/grep [-E | -F] [-c | -l | -q] [-bhinsvwx] -e pattern_list... [-f pattern_file]... [file]...
-f pattern_file Reads one or more patterns from the file named by the path name pattern_file. Patterns in pattern_file are terminated by a NEWLINE character. . . .
Watch out - there's two grep
s in solaris!