Using Bash scripting to compare arrays looking for matches


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using Bash scripting to compare arrays looking for matches
# 1  
Old 10-28-2009
Using Bash scripting to compare arrays looking for matches

I have two arrays I need to compare against a third, looking for matches, not differences. I think I'm going to have to convert the arrays to files and grep them, but I'm not too sure if there's a tool to enable me to matches specifically, instead of differences.

Thanks in advance!

Karl
# 2  
Old 10-28-2009
Why cannot you simply iterate through arrays and 'compare'?
# 3  
Old 10-28-2009
I was not aware that compare allowed only output of matches... Also, I'm afraid I'm not sure how I would iterate through an array.

My idea so far was to take the third array (shortlist) and compare it to the others one at a time by using the list as an input to grep. Conceptually:

Generate two long lists
Input into arrays
Generate short list
Input into array
Feed short list into grep and compare against the first long list
-No matches? Go on.
- Matches? Print and go on.
Compare against the second list
-Matches? Print and go on.
-No matches? Go on.

I have a script so far that generates the arrays from a few different files.

I guess I just need to figure out how to input a file, line by line, into grep so that grep would use the first line, and look for it in the array (or a file? can grep check arrays?) then take the second line, etc.
# 4  
Old 10-28-2009
you might not need arrays at all. Why don't you take a 'first long' list and a 'short list' and find the 'matches' - do the same with the 'second long list'.
If you post samples (short list and a one long list) and describe the 'matching' algorithm, we might help to get started.
Please use code tags when posting data samples. Also provide the desired result based on your sample input.
# 5  
Old 10-28-2009
So, let's see if I can construct an example.

Long list 1 contains a list of files, so does long list 2. These filenames are being compared to shortlist, which is a list generated from a find command used on a directory. The long lists contain files that ARE NOT allowed in the directory.

I had no idea what algorithm to use to look for matches; that was a large part of my question.

Code:
Long_List1=( $( cat /lists/badfiles ) )
Long_List2=( $( cat /lists/disallowedfiles.txt ) )
find /files -name *.stuff > /tmp/stuff.txt
short_list=( $( cat /tmp/stuff.txt ) )

I was thinking of either skipping reading the files into arrays and using short_list (stuff.txt) as input into grep, or using the arrays in the fashion specified above.
# 6  
Old 10-28-2009
depending on the actual content of files, here's one way:
Code:
grep -f /tmp/stuff.txt /lists/badfiles
grep -f /tmp/stuff.txt /lists/disallowedfiles.txt

# 7  
Old 10-28-2009
Thanks! Contents of the file is just a list of files, one per line:

Code:
badfiles.txt:

file.stuff
file2.stuff
file3.stuff

One of the long lists also has semicolons at the end of the line, and it has more information. I'll be needing to strip out the semicolons and things afterwards, but I need to compare first.

EDIT: Forgot to mention, I'm on a strange Solaris platform, the -f switch doesn't work for grep.

Partly the issue of why I'm asking for help. I'm used to a BSD/Mac os x platform, and Solaris is ridiculously wonky. Well, okay, maybe it's not so bad, I'm just not used to it... Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare bash arrays issue

Hello everyone, I need help comparing 2 arrays. the first array is static; the second array is not .. array1=( "macOS Mojave" "iTunes" ) cd /Volumes array2=( * ) # output of array2 macOS Mojave iTunes Mac me The problem occurs when I compare the arrays with the following code - ... (6 Replies)
Discussion started by: trexthurman
6 Replies

2. Shell Programming and Scripting

Compare two arrays by values [BASH]

I have 2 arrays of values for example A1 ={10 15 3 21} A2 ={10 15 3 22} I need to check which one has greater values. However: A1 ={10 15 3 21} A2 ={10 15 3 21 3} - this one would be greater. A1 ={10 15 5 21} - this one greater A2 ={10 15 3 21} Basically, I want to compare patch... (6 Replies)
Discussion started by: Jutsimitsu
6 Replies

3. Shell Programming and Scripting

BASH - Compare 2 Files, Output All Matches

This is probably rehash but I did look. :rolleyes: I want a bash script that will take Item 1 in File1, traverse all lines in File2 and output if a match exists. Continuing the pattern recursively, Item2, File1, traverse all lines in File2 for a match, continue this pattern until all lines... (6 Replies)
Discussion started by: rcbarr2014
6 Replies

4. Shell Programming and Scripting

Compare 2 files and print matches and non-matches in separate files

Hi all, I have two files, chap.txt and complex.txt. chap.txt looks like this: a d l m r k complex.txt looks like this: a c d e l m n j a d l p q r c p r m ......... (7 Replies)
Discussion started by: AshwaniSharma09
7 Replies

5. Shell Programming and Scripting

Compare two arrays

Hi, I am trying to compare two lists that are held in two variables so I believe I need to access the array elements to compare these. I am using ksh 88 and the code I have tried is below: for file in ${origfilelist} do if ]] then print -- "File ${file}... (3 Replies)
Discussion started by: frodo61
3 Replies

6. Shell Programming and Scripting

Bash arrays that compare ip addresses.

I've been trying to have an array of ip addresses go through a loop one at a time. Then compare if the current element is in another array of ip addresses. I've traced my error with /bin/bash -x + for c in '"${ip}"' ./netk5: line 65: 50.17.231.23 23.64.146.110 23.64.159.139 107.14.36.129... (17 Replies)
Discussion started by: Azrael
17 Replies

7. Shell Programming and Scripting

Using arrays in bash using strings to bash built-in true

I have the following code and for some reason when I call the program using /home/tcdata/tatsh/trunk/hstmy/bin/bash/raytrac.bash --cmod=jcdint.cmod I get hasArgument = hasArgument = true Somehow the array element is returning even though I have not chosen the option. ... (41 Replies)
Discussion started by: kristinu
41 Replies

8. Shell Programming and Scripting

Compare two text files and print matches

Hi, I am looking for a way to compare two text files and print the matches. For example; File1.txt 89473036 78474384 48948408 95754748 47849030 File2.txt 47849030 46730356 16734947 78474384 36340047 Output: (11 Replies)
Discussion started by: lewk
11 Replies

9. Shell Programming and Scripting

Bash scripting to compare N number of files located in two directories

I want to compare "N" (around 2000+) number of huge files located in a directory A against "N" files located in a different directory using Bash scripting. Please help me with any scripts available. Thanks. (2 Replies)
Discussion started by: Sangtha
2 Replies

10. Shell Programming and Scripting

Compare two arrays in sh or compare two fields

I want a soultion to compare two arrays in sh with an easy way.I want a solution to synchrose users between different AIX servers where no NIS is available. All users are meant to be same on all 10 servers. So the approach is to consider first server as master user repository and whatever the users... (0 Replies)
Discussion started by: rijeshpp
0 Replies
Login or Register to Ask a Question