I'm a beginner to Perl and programming in general...and need some help.
I have two files, both containing a list of accounts. File 1 has one column, containing the list of accounts and File 2 has 5 columns including filename, accounts, and 3 other nonrelevant columns. I want to retrieve the accounts that're only in File2 (in other words, remove from File2 what is in File1 also)and output the filename that's associated with that account to another file. How can I do this?
Update:
Thank you for your prompt reply. I am also having troubling with the code as it's not compiling.
I have the following errors after I've made the necessary changes to the die statement...
Also, FILE_2 does not necessarilyy contain account numbers in column2, rather the columns are in the following order:syntax error at test.pl line 11, near "$accounts exists" syntax error at test.pl line 14, near "}"
and in this case, the 3 nonrelevant columns are LOC, ST, CO...will the following line work if I make these changes to column order?FILENAME LOC ST CO ACCOUNT
I also didn't quite understand what's being done in this statement:my ( $filename, @nonrelevant, $accounts ) = split( /\t/, $line );
But I need to output the FILENAME column(for accounts that exist only in FILE_2) into a third file.next if ( $accounts exists( @{[<FILE>]} ) );
Thanks!
#!/usr/bin/perl use strict; use warnings; open FILE_1, 'file1.txt' || die "ERROR:\t$!\n"; open FILE_2, 'file2.txt' || die "ERROR:\t$!\n"; while ( my $line = <FILE_2> ) { my ( $filename, $accounts, @nonrelevant ) = split( /\t/, $line ); next if ( $accounts exists( @{[<FILE>]} ) ); print "$accounts:\t$filename\n"; } close FILE_1; close FILE_2;
20051127 Edit by ysth: prepend original question
In reply to removing same entries between 2 files by sbp
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |