Compare Array results


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare Array results
# 1  
Old 11-26-2008
Compare Array results

Hi,
In a kshell , i need to compare the results of two array .
each Non-match value should copy to a new array.

For example:
==========
Array one contain the following values:
A
B
C

Array two contain the following values:
C
F
A
E

After comparing this arrays , a new array should be populated with the following values:
F
E

I will appreciate you help
Thank You Smilie
# 2  
Old 11-26-2008
Hammer & Screwdriver What about grep on two files

Not sure of the source of those values, but if they are in files then the following will provide your required output.

Code:
> cat file82
A
B
C
> cat file83
C
F
A
E
> egrep -vf file82 file83
F
E

# 3  
Old 11-26-2008
Compare Array

Hi joeyg,

The values are not stored in a file, they are stored inside an Array.
I need to compare the arraies values , and create new array based on the unmached values.

Thank Again
# 4  
Old 11-26-2008
As ksh array:

Code:
root@isau02:/data/tmp/testfeld> cat ./mach.ksh
#!/usr/bin/ksh

set -A array1 "A" "B" "C"
set -A array2 "C" "F" "A" "E"

NUM_1=`echo ${#array1[*]}`

for ELE2 in ${array2[*]}; do
                Z=0
        for ELE1 in ${array1[*]}; do
                if [[ $ELE1 != $ELE2 ]]; then
                        let Z=$Z+1
                        if [[ $Z = $NUM_1 ]]; then
                                echo $ELE2
                        fi
                fi
        done
done

exit 0
root@isau02:/data/tmp/testfeld> ./mach.ksh
F
E

TBH, I would tend to write the stuff to temp files and use Joeyg's solution.
# 5  
Old 11-30-2008
Hi Zaxxon,
You help was very valueable for me.
I need some extra help :

I need to add the result (e.g : F E) to the first Array.
So, the actual result should be :
A B C F E (the order is not important).

I tried to use set +A as followed but didnt got therewuired results :

root@isau02:/data/tmp/testfeld> cat ./mach.ksh
#!/usr/bin/ksh

set -A array1 "A" "B" "C"
set -A array2 "C" "F" "A" "E"

NUM_1=`echo ${#array1[*]}`

for ELE2 in ${array2[*]}; do
Z=0
for ELE1 in ${array1[*]}; do
if [[ $ELE1 != $ELE2 ]]; then
let Z=$Z+1
if [[ $Z = $NUM_1 ]]; then
echo $ELE2
set +array1 $ELE2 ====> the change i added
fi
fi
done
done

exit 0

Thanks Again
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Results Of A Variable Into An Array Using C Language

Can C add its results into an array like bash? For example using bash: cat /etc/passwd **truncated for space ** gdm:x:109:118:Gnome Display Manager:/var/lib/gdm:/bin/false mysql:x:110:122:MySQL Server,,,:/nonexistent:/bin/false statd:x:111:65534::/var/lib/nfs:/bin/false... (5 Replies)
Discussion started by: metallica1973
5 Replies

2. Shell Programming and Scripting

Append awk results into file or array

for a in {1..100} do awk '{ sum+=$a} END {print sum}' a=$a file1 > file2 done I know I will get only one number if following the code above, how can I get 100 sum numbers in file2? (2 Replies)
Discussion started by: wanliushao
2 Replies

3. Shell Programming and Scripting

Storing the SQL results in array variables

Requirement 1) I need to execute 15 SQL queries in oracle through linux script. All these query results needs to be stored in array variables. Requirement 2) And these 15 queries needs to be executed in parallel. Requirement 3) Once all the queries executed then the shell script should... (3 Replies)
Discussion started by: Niranjancse
3 Replies

4. Shell Programming and Scripting

Compare file to array, replace with corresponding second array

ok, so here is the issue, I have 2 arrays. I need to be able to create a loop that will find ${ARRAY1 in the text doc, and replace it with ${ARRAY2 then write the results. I already have that working. The problem is, I need it to do that same result across however many items are in the 2... (2 Replies)
Discussion started by: gentlefury
2 Replies

5. Shell Programming and Scripting

compare two array

Hi firnds, I am having two arrays eg. @a=qw(1 2 3 4) and @b=qw(1 3 6 9) Now, I want to compare two arrays on first index of both arrays and if they matched, it should output remaining indexes Pseudo-code if ($a == $b) { print "remaining indexes";} How can i do this using perl? (1 Reply)
Discussion started by: Renesh
1 Replies

6. Shell Programming and Scripting

compare two value in array - Perl

Hi, I just wondering if I want to compare previous value to next value if it same count + 1 if not doesn't count how do we compare in Perl? Thank (2 Replies)
Discussion started by: guidely
2 Replies

7. Shell Programming and Scripting

Adding results of a find to an array

I'm trying to add the paths of all the xml files in certain directories to an array. I want to use the array later in my code. Anyway, for some reason this isn't working. Any help would be appreciated. Path_Counter=0 for result in "find * -name '*.xml'"; do XmlPath="$result" echo... (2 Replies)
Discussion started by: Fly_Moe
2 Replies

8. Shell Programming and Scripting

Shell script compare all parameters in two files and display results

Hi , I am not familiar with shell programming. I have a requirement like i have two files .I need to compare the two files by comparing each parameter and i should produce 2 outputs. 1)i have around 35 parameters say i have one parameter name called db_name=dcap in one file and... (7 Replies)
Discussion started by: muraliinfy04
7 Replies

9. Shell Programming and Scripting

variable getting results from a log file; compare it in an IF cycle

Hi All I am going crazy trying to resolve this easy task.... I have a log file that is created by xcode; and it contains the results of a build (either success or fail); what i am trying to achieve here is to get the result from the log, and check if is a pass or fail in an IF statement, so... (3 Replies)
Discussion started by: newbiez
3 Replies

10. Shell Programming and Scripting

Results of command execution into array

Hi Can anybody tell me how can I dump the results of execution of a command into array form? For example, I want to execute: and put each part of the result in an array element: Thanks (2 Replies)
Discussion started by: alirezan
2 Replies
Login or Register to Ask a Question