How to get difference of two array?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get difference of two array?
# 1  
Old 01-22-2014
How to get difference of two array?

Required Unix shell script which will identify the difference of String2 based on string1. I am facing this issue and unable to achieve the result.
Code:
String1= {1 3 lok kam}
String2= {3 kam 5}

Result should be like below:
Code:
Data is matching for: 3
Data is matching for: kam
Data is not matching for: 5

Thanks in advance.

Last edited by vbe; 01-22-2014 at 10:49 AM.. Reason: rm fancy fonts and colour + code tags
# 2  
Old 01-22-2014
Quote:
Originally Posted by lokendrasb
Required Unix shell script which will identify the difference of String2 based on string1. I am facing this issue and unable to achieve the result.
Code:
String1= {1 3 lok kam}
String2= {3 kam 5}

Result should be like below:
Code:
Data is matching for: 3
Data is matching for: kam
Data is not matching for: 5

Thanks in advance.

Here is one way to do so

Code:
#!/bin/bash

String1="1 3 lok kam"
String2="3 kam 5"

for i in ${String2[@]}; do
    grep $i <<<$String1 >/dev/null
    [ $? -eq 0 ] && echo "Data is matching for:$i" || echo "Data is not matching for: $i"
done

Code:
$ bash test
Data is matching for:3
Data is matching for:kam
Data is not matching for: 5

# 3  
Old 01-22-2014
grep accepts partial match, why not set indicator for no match, iterate string1 inside string 2 and check for =, if match print matching, set indicator and break, and after inner loop, if indicator says no match, print no match.
# 4  
Old 01-22-2014
Yes. DGPickett I did a mistake

Code:
#!/bin/bash

String1="1 3 lok kam"
String2="3 kam 5"

for i in ${String2[@]}; do
   f=0
   for j in ${String1[@]}; do 
    	[ "$i" == "$j" ] && f=1 && break
   done
   [ "$f" -eq "1" ] && echo "Data is matching for: $i" || echo "Data is not matching for: $i"
done

This User Gave Thanks to Akshay Hegde For This Post:
# 5  
Old 01-23-2014
With actual arrays (bash, ksh):
Code:
#!/bin/bash
array1=(1 3 lok kam)                                                              
array2=(3 kam 5)                                                              
for i in "${array2[@]}"
do
  for j in "${array1[@]}"
  do
    if [ "$i" = "$j" ]
    then
      printf "Data is matching for: %s\n" "$i"
      continue 2
    fi
  done
  printf "Data is not matching for: %s\n" "$i"
done


--
@Akshay: for i in $String1; do will do the same thing...

Last edited by Scrutinizer; 01-23-2014 at 02:25 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 01-23-2014
Hello,

For ksh it should work like the following.

Code:
array1="1 3 lok kam"                                                           
array2="3 kam 5"
                                                     
set -A array2_array2 $array2         
set -A array1_array1 $array1
 
for i in "${array2_array2[@]}"
do
  for j in "${array1_array1[@]}"
  do
    if [ "$i" = "$j" ]
    then
      printf "Data is matching for: %s\n" "$i"
      continue 2
    fi
  done
  printf "Data is not matching for: %s\n" "$i"
done

Output will be as follows.

Code:
Data is matching for: 3
Data is matching for: kam
Data is not matching for: 5


Thanks,
R. Singh
# 7  
Old 01-23-2014
Thanks to everyone. now I am running my code successfully
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to calculate difference of split and sum the difference

In the awk I am trying to subtract the difference $3-$2 of each matching $4 before the first _ (underscore) and print that value in $13. I think the awk will do that, but added comments. What I am not sure off is how to add a line or lines that will add sum each matching $13 value and put it in... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Bash arrays: rebin/interpolate smaller array to large array

hello, i need a bit of help on how to do this effectively in bash without a lot of extra looping or massive switch/case i have a long array of M elements and a short array of N elements, so M > N always. M is not a multiple of N. for case 1, I want to stretch N to fit M arrayHuge H = (... (2 Replies)
Discussion started by: f77hack
2 Replies

3. Shell Programming and Scripting

Finding difference in between two array's of strings

Hi, Can anybody help me in finding the difference between two array elements with the help of code pls. purge=("Purge Concurrent Request and/or Manager Data" "Purge Signon Audit data" "Purge Obsolete Workflow Runtime Data" "Purge Logs and Closed System Alerts") purge_1=("Purge Obsolete... (3 Replies)
Discussion started by: Y.balakrishna
3 Replies

4. Shell Programming and Scripting

Bash 3.2 - Array / Regex - IF 3rd member in array ends in 5 digits then do somthing...

Trying to do some control flow parsing based on the index postion of an array member. Here is the pseudo code I am trying to write in (preferably in pure bash) where possible. I am thinking regex with do the trick, but need a little help. pesudo code if == ENDSINFIVEINTS ]]; then do... (4 Replies)
Discussion started by: briandanielz
4 Replies

5. Programming

what is the main difference between difference between using nonatomic lseek and O_APPEND

I think both write at the end of the file ...... but is there a sharp difference between those 2 instruction ..... thank you this is my 3rd question today forgive me :D (1 Reply)
Discussion started by: fwrlfo
1 Replies

6. Shell Programming and Scripting

check the difference between 2 array

Hello Thanks everyone for the help earlier, what I would like to learn now is how can I achieve the following : array1 = (1234567,7665456,998889,000909) array2 = (1234567,5581445,998889,000909) Result 5581445 doesn't exist on array1 Thank you (6 Replies)
Discussion started by: amlife
6 Replies

7. Shell Programming and Scripting

PERL : Read an array and write to another array with intial string pattern checks

I have an array and two variables as below, I need to check if $datevar is present in $filename. If so, i need to replace $filename with the values in the array. I need the output inside an ARRAY How can this be done. Any help will be appreciated. Thanks in advance. (2 Replies)
Discussion started by: irudayaraj
2 Replies

8. Shell Programming and Scripting

PHP: Search Multi-Dimensional(nested) array and export values of currenly worked on array.

Hi All, I'm writing a nagios check that will see if our ldap servers are in sync... I got the status data into a nested array, I would like to search key of each array and if "OK" is NOT present, echo other key=>values in the current array to a variable so...eg...let take the single array... (1 Reply)
Discussion started by: zeekblack
1 Replies

9. Shell Programming and Scripting

Array Difference Sun Vs Linux

Hi All, I have a script in which an array is defined. when i run that on Linux box its fine but when i run on SunOS its points to the line where array is defined as below : syntax error at line 9 : `(' unexpected array defined as ID=( ~Hog ~Todd ~Mike ) Thanks in advance (0 Replies)
Discussion started by: ravi.sadani19
0 Replies

10. Programming

Creating an array to hold posix thread ids: Only dynamic array works

I am facing a strange error while creating posix threads: Given below are two snippets of code, the first one works whereas the second one gives a garbage value in the output. Snippet 1 This works: -------------- int *threadids; threadids = (int *) malloc (num_threads * sizeof(int)); ... (4 Replies)
Discussion started by: kmehta
4 Replies
Login or Register to Ask a Question