Comparison between two arrays


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparison between two arrays
# 1  
Old 01-13-2015
Comparison between two arrays

Hello,

I am trying to compare two arrays and return common values. The purpose is to find running databases oracle running on the atually operating system. I have found a way to do it but it takes to long.

Code:
list=`cat /etc/oratab | grep "/home/ora/app/oracle/product/e.11.2.0.1.0" | cut -f 1 -d :`;

Output: INST3

Code:
list_run_inst=`ps -ef | grep ora | awk '{print $8}' | awk -F '_' '{print $3}' | sort -u`;

Output: INST1 INST2 INST3

At this point we have two arrays:

list = (INST1);
list_run_inst = (INST1 INST2 INST3)
;

The way I found to compare two arrays was:
- Join the two arrays and using awk command find duplicates and print them.

Code:
combined=("${list[@]}""${list_run_inst[@]}");

finalResult=`printf '%s\n' "${combined[@]}"|awk '!($0 in seen){seen[$0];next} 1'`;

for inst in $finalResult;do echo "$inst," ;done;

Could you help me to improve this code? I have tried to compare the two arrays/lists and retrieve the common values but for some reason I couldn't, there's always missing something Smilie

Any suggestion or idea is welcome. Thanks for your help.

Last edited by rafazz; 01-13-2015 at 09:30 AM..
# 2  
Old 01-13-2015
Not clear. Samples would help. Did you consider the comm command?
# 3  
Old 01-13-2015
Quote:
Originally Posted by RudiC
Not clear. Samples would help. Did you consider the comm command?
Hi Rudic. Thanks for your reply. I have edit the case with samples.
I have two arrays for example arrayA and array B. How can I compare the results?

arrayA = (item1)
array B = (item1 item2 item3)

Using comm it is not retrieving the results as supposed. Probably lack of knowledge but comm is to compare two files and compares by lines. I have tried to compare the two lists but how can I print the two lists at the same time in order to compare?

Thanks.
# 4  
Old 01-13-2015
Don't create variables or array; use the commands' output to create files and comm those. Or like so:
Code:
sort file[12]|uniq -d
INST1

---------- Post updated at 14:52 ---------- Previous update was at 14:44 ----------

Try
Code:
{ awk '/\/home\/ora\/app\/oracle\/product\/e.11.2.0.1.0/ {print $1}' FS=":"  /etc/oratab;
  ps -ef | awk '/ora/ {split($8, TMP, "_"); print TMP[3]}'; } | sort | uniq -d

I can't test as I don't have your setup...
# 5  
Old 01-13-2015
Thanks for your help. The suggested code is returning results which are not true unfortunately Smilie

I cannot use the arrays for comparing? I need to be as inlinescript to call it via command line.
# 6  
Old 01-13-2015
WHAT is not true? Post the outputs of every subcommand.

Comparing shell arrays will need some script lines; might not be shorter than above...
# 7  
Old 01-14-2015
Sorry for delay! I was trying to sort this out. The output is the following:

Code:
list=`cat /etc/oratab | grep "/home/ora/app/oracle/product/e.11.2.0.1.0" | cut -f 1 -d :`;

Output:
INS4
INS2
INS1
INS5
INS7
INS3
INS6
INS8
INS9

Code:
list_run_inst=`ps -ef | grep ora | awk '{print $8}' | awk -F '_' '{print $3}' | sort -u` | grep -v '^ *$';

Output:
INS4
INS2
INS1
INS7
INS3


Here you have the two lists and we will start to compare. If they match/exist on both lists, print. If not, skip.

I have developed two codes to make the compare of this two outputs:

1.
Code:
list=`cat /etc/oratab | grep "/home/ora/app/oracle/product/e.11.2.0.1.0" | cut -f 1 -d :`;list_run_inst=`ps -ef | grep ora | awk '{print $8}' | awk -F '_' '{print $3}' | sort -u`;combined=("${list[@]}""${list_run_inst[@]}");finalResult=`printf '%s\n' "${combined[@]}"|awk '!($0 in seen){seen[$0];next} 1'`;for inst in $finalResult;do echo "$inst," ;done;

2.
Code:
list=`cat /etc/oratab | grep "/home/ora/app/oracle/product/e.11.2.0.1.0" | cut -f 1 -d :`;list_run_inst=`ps -ef | grep ora | awk '{print $8}' | awk -F '_' '{print $3}' | sort -u | sed '/^\s*$/d'`;intersections=();for item1 in "${arr_list[@]}"; do for item2 in "${arr_run[@]}"; do if [[ $item1 == "$item2" ]]; then intersections+=( "$item1" );break;fi;done;done;for inst in ${intersections[@]};do echo "$inst,";done;

I dont know if is something missing but both sometimes return wrong results.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Arrays

Am using bash For eg: Suppose i have a array arr=(1 2 3 4 5 6 7 8 9 10 11 12) suppose i give input 5 to a script and script should able to print values greater than or equal to 5 like below: Input: 5 output: 5,6,7,8,9,10,11,12 (7 Replies)
Discussion started by: manid
7 Replies

2. Shell Programming and Scripting

Using arrays?

I have never used arrays before but I have a script like this: var1=$(for i in $(cat /tmp/jobs.021013);do $LIST -job $i -all | perl -ne 'print /.*(\bInfo.bptm\(pid=\d{3,5}).*/' | tr -d "(Info=regpid" | tr -d ')'; $LIST -job $i -all | cut -f7 -d','| sed -e "s/^\(*\)\(*\)\(*\)\(.*\)/\1... (2 Replies)
Discussion started by: newbie2010
2 Replies

3. Programming

Arrays in C++

I've noticed something interesting in C++ programming. I've always done tricky stuff with pointers and references to have functions deal with arrays. Doing exercises again out of a C++ book has shown me an easier way, I didn't even know was there. It's weird to me. When dealing with arrays, it... (4 Replies)
Discussion started by: John Tate
4 Replies

4. Shell Programming and Scripting

How can I use the arrays ?

Hi all, I have a file test1.txt with the below contents abc def ghj xyz I tried printing these values using arrays. Script tried : =========== set -A array1 `cat test1.txt` count=${#array1 } i=0 while do echo "element of array $array1" done (1 Reply)
Discussion started by: dnam9917
1 Replies

5. Programming

question about int arrays and file pointer arrays

if i declare both but don't input any variables what values will the int array and file pointer array have on default, and if i want to reset any of the elements of both arrays to default, should i just set it to 0 or NULL or what? (1 Reply)
Discussion started by: omega666
1 Replies

6. Shell Programming and Scripting

$((...)) and $[...] comparison

Does $((mathematical expression)) and $ mean the same? (7 Replies)
Discussion started by: proactiveaditya
7 Replies

7. UNIX for Dummies Questions & Answers

arrays how to?

Hello, I am some what of a newbie to awk scripting and I seem to be struggling with this problem. I know I need to use arrays but I can't figure out how to use them. I have an input file that looks like this; Name,Team,First Test, Second Test, Third Test Crystal,Red,5,17,22... (1 Reply)
Discussion started by: vlopez
1 Replies

8. Web Development

PHP arrays in arrays

PHP question... I have an SQL query that's pulled back user IDs as a set of columns. Rather than IDs, I want to use their names. So I have an array of columns $col with values 1,7,3,12 etc and I've got an array $person with values "Fred", "Bert", "Tom" etc So what I want to do is display the... (3 Replies)
Discussion started by: JerryHone
3 Replies

9. Shell Programming and Scripting

Arrays

Dear all, How can i unset arrays. I mean all the subscripts including the array after using them. Could you direct me to some links of array memory handling in the korn shell. Thanks (2 Replies)
Discussion started by: earlysame55
2 Replies

10. Shell Programming and Scripting

need some help..Comparison

I need some help which would probably be for most of you a simple script. I need to read in the data from a .dat file and then compare avg to see who is the highest avg. Here is my script so far. #!/bin/ksh #reading in the data from lab3.dat filename=$1 while read name o1 o2 o3 o4 o5 o6... (0 Replies)
Discussion started by: bluesilo
0 Replies
Login or Register to Ask a Question