check the difference between 2 array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting check the difference between 2 array
# 1  
Old 01-27-2012
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
# 2  
Old 01-27-2012
Please show us what you have done so far.

You should have the logic and ask for help on which commands to use.
# 3  
Old 01-27-2012

Usng bash:
Code:
array1=(1234567 7665456 998889 000909)
array2=(1234567 5581445 998889 000909)

grep -v -f <(printf "%s\n" "${array1[@]}")  <(printf "%s\n" "${array2[@]}")

# 4  
Old 01-30-2012
Quote:
Originally Posted by cfajohnson

Usng bash:
Code:
array1=(1234567 7665456 998889 000909)
array2=(1234567 5581445 998889 000909)
grep -v -f <(printf "%s\n" "${array1[@]}")  <(printf "%s\n" "${array2[@]}")

Ran above code using bash and received the following error:
Code:
grep: /dev/fd/62: No such file or directory

The following code works:
Code:
array1=(1234567 7665456 998889 000909)
array2=(1234567 5581445 998889 000909)
printf "%s\n" "${array2[@]}" | grep -v "$(printf "%s\n" "${array1[@]}")"

# 5  
Old 01-30-2012
Quote:
Originally Posted by Shell_Life
Ran above code using bash and received the following error:
Code:
grep: /dev/fd/62: No such file or directory

The following code works:
It worked for me though!
Code:
root@bt> bash --version
GNU bash, version 4.1.5(1)-release (i486-pc-linux-gnu)

root@bt> array1=(1234567 7665456 998889 000909)
root@bt> array2=(1234567 5581445 998889 000909)
root@bt> grep -v -f <(printf "%s\n" "${array1[@]}")  <(printf "%s\n" "${array2[@]}")
5581445

--ahamed
# 6  
Old 01-30-2012
I used:
Code:
GNU bash, version 3.2.0(1)-release (x86_64-unknown-linux-gnu)

As much as possible, if one solution can be given in a more universal usage, then we must provide it.

Otherwise, for the same requirement, there will be several possible solutions, based on which OP (linux, unix), shell, shell version, etc.

Let's be generic as much as we can.
# 7  
Old 01-30-2012
Hi.

See also check the difference between 2 arrays

Best wishes ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Array Elements Check

Hi everyone, :) I'm trying to make a simple C program that scans an array of chars to see if its elements are similar. I can't understand what's wrong. Could you help me to fix this? Here is the code. Thanks! #include<stdio.h> int main() { int arr; int i, len; int flag =... (10 Replies)
Discussion started by: IgorGest
10 Replies

2. Shell Programming and Scripting

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. String1= {1 3 lok kam} String2= {3 kam 5} Result should be like below: Data is matching for: 3 Data is matching for: kam Data is not matching... (7 Replies)
Discussion started by: lokendrasb
7 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

Check/Parse log file's lines using time difference/timestamp

I was looking at this script which outputs the two lines which differs less than one sec. #!/usr/bin/perl -w use strict; use warnings; use Time::Local; use constant SEC_MILIC => 1000; my $file='infile'; ## Open for reading argument file. open my $fh, "<", $file or die "Cannot... (1 Reply)
Discussion started by: cele_82
1 Replies

5. UNIX for Dummies Questions & Answers

ksh to check second time difference between two servers

I am currently setting up a public key authentication between servers. The goal is to get the date via `ssh hostname date` on all the 4 remote servers , put the value in a text file on the central server and compare the date (specifically seconds) for each server date output to check if time is... (7 Replies)
Discussion started by: depam
7 Replies

6. Red Hat

How to check for values in array?

i stored some values in array , then i traverse through the array and check for some values and if they exist then echo success. let us consider that in our array we stored values from an sql query like this #!/bin/bash declare -a arr arr=$( sqlplus -s rte/rted2@rel76d2 << EOF set... (1 Reply)
Discussion started by: ramsavi
1 Replies

7. Shell Programming and Scripting

Comparing date and check minute difference

Dear all, I'm stuck on Solaris 9 bash (I believe is quite different from Linux system) trying to find a solution. I have one file named like:120629-1750-TERZ81_AS_YTR.txt YYMMDD-HH-MM-......The script should compare the actual date with the one reported on the file name and then take an action... (4 Replies)
Discussion started by: Lord Spectre
4 Replies

8. UNIX for Dummies Questions & Answers

Check for difference in output of 2 commands?

Hello! I'm just learning the shell, and I would really like to know how to do this: Given these 2 commands: ls -l ls -le How can I, with a one-liner, ask the shell to show me visually in the shell, what the difference is between the output of the two commands? They look the same to me... (6 Replies)
Discussion started by: turbofayce
6 Replies

9. Shell Programming and Scripting

Help! Yet another check element in array Question

Greetings, DISCLAIMER: My shell scripting is rusty so my question may be borderline stupid. You've been warned. I need to create a script that a) lists the content of zip files in a directory and b) sends out an `exception` report. My ZIP files contain a control file (for load check). I want... (2 Replies)
Discussion started by: alan
2 Replies

10. 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
Login or Register to Ask a Question