Comparing 2 arrays but ignoring certain patterns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing 2 arrays but ignoring certain patterns
# 1  
Old 06-28-2012
Comparing 2 arrays but ignoring certain patterns -Perl

I'm trying to compare 2 array and print the difference at a 3rd file. However how am i going to compare this 2 arrays by ignoring certain patterns:

For example:
1st array contains:
Code:
ctahci
cptcsa0
ctsata:25:seed
cptcsa1:50:seed
ctsata_1:25:seed

2nd array contains:
Code:
cptcsa0
ctsata
cptcsa1

I wanna ignore the ":25:seed" and ":50:seed" in the @removelist during comparison.

So that the output file will be:
Code:
ctahci
ctsata_1:25:seed

This is the code for me to find the difference but i got no idea on how to ignore the 2 patterns.
Code:
 #!/usr/intel/bin/perl

@testlist = qw(ctahci,cptcsa0,ctsata:25:seed,cptcsa1:50:seed,ctsata_1:25:seed)
@removelist =qw(cptcsa0,ctsata,cptcsa1)
my $outfile = "./new.list";

%removelist=map{$_=>1} @removelist;

@newlist=grep(!defined $removelist{$_}, @testlist);

# creating new list
open (OUTFILE, ">$outfile") or die "Cannot open $outfile for writing \n";
);
print OUTFILE "$_\n" foreach (@newlist);;

Any ideas pls help. Thx

Last edited by hongping; 06-28-2012 at 11:27 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Linux/Shell script - How to compare 2 arrays based on patterns and get the differences

I have FILE 1 (This file has all master columns/headers) A|B|C|D|E|F|G|H|STATUS FILE 2 A|C|F|I|OFF_STATUS 3|4|5|4|Y 6|7|8|5|Y Below command give me all headers of FILE 2 into array2.txt file paste <(head -1 FILE2.txt | tr '|' '\n')>array2.txt So I would like to compare... (2 Replies)
Discussion started by: jmadhams
2 Replies

2. Shell Programming and Scripting

awk arrays comparing multiple columns across two files.

Hi, I'm trying to use awk arrays to compare values across two files based on multiple columns. I've attempted to load file 2 into an array and compare with values in file 1, but success has been absent. If anyone has any suggestions (and I'm not even sure if my script so far is on the right lines)... (4 Replies)
Discussion started by: hubleo
4 Replies

3. Shell Programming and Scripting

ignoring multiple patterns

Hi, Using grep how to ignore multiple patterns for example # multiple #installOption #role role #ROLE I want to ignore all that comes with # in a single command If I use grep -v \^\#"role" <filename> (2 Replies)
Discussion started by: dbashyam
2 Replies

4. UNIX for Dummies Questions & Answers

Comparing lists.. Arrays maybe?

Problem Part 1. Gather data from linux server and output to a file named data_DDMMYY Output file must contain the file name and size Part 2. Compare todays data_DDMMYY to yesterdays data_DDMMYY and output results to a file named difference_DDMMYY Output file must show the difference in... (3 Replies)
Discussion started by: Aussiemick
3 Replies

5. Programming

question about int arrays and file pointer arrays

if i declare both but don't input any variables what values will the int array and file pointer array have on default, and if i want to reset any of the elements of both arrays to default, should i just set it to 0 or NULL or what? (1 Reply)
Discussion started by: omega666
1 Replies

6. Shell Programming and Scripting

PERL: simple comparing arrays question

Hi there, i have been trying different methods and i wonder if somebody could explain to me how i would perform a comparison on two arrays for example my @array1 = ("gary" ,"peter", "paul"); my @array2 = ("gary" ,"peter", "joe"); I have two arrays above, and i want to something like this... (5 Replies)
Discussion started by: hcclnoodles
5 Replies

7. Shell Programming and Scripting

Comparing arrays in perl

Hi all, I am trying to compare two arrays in perl using the following code. foreach $item (@arrayA){ push(@arrayC, $item) unless grep(/$item/, @arrayB); ... (1 Reply)
Discussion started by: chriss_58
1 Replies

8. Shell Programming and Scripting

comparing two arrays or strings in bash

Hi there, im having issue with comparing two variables, in a bash script. im trying to do the following: - get a word from user 1 - split the word into array - get a character from user2 trying to compare the character entered by user 2 with every single character in the array... (2 Replies)
Discussion started by: new2Linux
2 Replies

9. Shell Programming and Scripting

Comparing Arrays?

Is there anyway that I can compare two Arrays to see if any new strings have been added in them? eg: Array 1: Joe Bob Jane Array 2: Joe Bob Jane Greg It would then output a new array with the changes: Array 3: Greg I'm not very good at shell scripting, and my google and forum searches... (4 Replies)
Discussion started by: blckleprd
4 Replies

10. UNIX for Dummies Questions & Answers

Ignoring Some Text When Comparing 2 Files

Hi Guys, How can I compare 2 text files and show the differences between the 2 files but have some of the text ignored? Example File1 File2 Thomas Thomass !1st name! David Davidd !2nd name! John John !3rd name! So,... (1 Reply)
Discussion started by: jimmyflip
1 Replies
Login or Register to Ask a Question