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.
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.
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.
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.
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.
Thanks! Contents of the file is just a list of files, one per line:
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...
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)