Divakar has asked for the wisdom of the Perl Monks concerning the following question:
Hi Folks,
i have 2 files (file1.txt and file2.txt). i have to check which things are newly added in file2.txt. so i am using grep function to check whether it is already there. but it is not matching exact string. anyone have any idea?
file1.txt contains
file2.txt containsnt64osbld2tmp vm-nt64osbld2 vm-nt64osbld3 vm-nt64osbld4 vm-nt64osbld5 vm-nt64osremot1 vm-nt64osremot2 vm-nt64osremot3 vm-nt64osremot4 vm-nt64osremot5 vm-nt64osremot6 vm-nt64osremot7 vm-nt64osremot8 vm-ntdivakar1 vm-ntdivakar2 vm-ntdivakar4 vm-ntosbld1 vm-ntosbld5 vm-ntoscert3 vm-ntosdev1 vm-ntskommare1 vm-ntskommare2 vm-ntskommare4 vm-os2k8r264-01 vm-osremote1 vm-osremote10 vm-osremote2 vm-osremote3 vm-osremote4 vm-osremote5 vm-osremote6 vm-osw2k8-1 vm-osw2k8-2 vm-oswin2k3-32 vm-oswin2k3-64
nt64osbld2 nt64osbld2tmp nt64osbld3 nt64oscitrix1 NTOSBLD4 ntosbld5 ntosbld6 VM-NT64OSBLD2 VM-NT64OSBLD3 VM-NT64OSBLD4 VM-NT64OSBLD5 vm-nt64osremot1 vm-nt64osremot2 vm-nt64osremot3 vm-nt64osremot4 vm-nt64osremot5 vm-nt64osremot6 VM-NT64OSREMOT7 VM-NT64OSREMOT8 vm-ntosbld1 VM-NTOSBLD5 vm-ntoscert3 vm-ntosdev1 VM-OS2K8R264-01 vm-osremote1 vm-osremote10 vm-osremote2 vm-osremote3 vm-osremote4 VM-OSREMOTE5 vm-osremote6 VM-OSW2K8-1 VM-OSW2K8-2 VM-OSW2K8X64-1 VM-OSW2K8X64-2 VM-OSWIN2K3-32 VM-OSWIN2K3-64
below is my script.
use warnings; use strict; my @first_list; my @second_list; my @first_list_new; my @second_list_new; open FIRST_LIST, "< first_list.txt" or print $! "\n"; @first_list=<FIRST_LIST>; close (FIRST_LIST); chomp (@first_list); open SECOND_LIST, "< second_list.txt" or print $! "\n"; @second_list=<SECOND_LIST>; close (SECOND_LIST); chomp (@second_list); foreach my $machine (@first_list) { my $machine_new=lc($machine); push(@first_list_new,$machine_new); } foreach my $machine (@second_list) { my $machine_new=lc($machine); push(@second_list_new,$machine_new); } print "Machines extra in second list\n\n"; foreach my $unique (@second_list_new) { if (grep (/^$unique$/,@first_list_new)) { print "already there $unique\n\n"; } else { print "newly in this $unique\n\n"; } }
Thanks & Regards,
Divakar
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to grep exact string
by ColonelPanic (Friar) on Nov 15, 2012 at 08:46 UTC | |
by tobyink (Canon) on Nov 15, 2012 at 09:06 UTC | |
by Kenosis (Priest) on Nov 15, 2012 at 22:14 UTC | |
by ColonelPanic (Friar) on Nov 15, 2012 at 09:34 UTC | |
by tobyink (Canon) on Nov 15, 2012 at 10:13 UTC | |
|
Re: How to grep exact string
by tobyink (Canon) on Nov 15, 2012 at 07:18 UTC | |
by Divakar (Sexton) on Nov 15, 2012 at 08:04 UTC | |
by tobyink (Canon) on Nov 15, 2012 at 08:49 UTC | |
by frozenwithjoy (Priest) on Nov 15, 2012 at 08:55 UTC | |
by frozenwithjoy (Priest) on Nov 15, 2012 at 08:29 UTC | |
by tobyink (Canon) on Nov 15, 2012 at 08:38 UTC | |
by frozenwithjoy (Priest) on Nov 15, 2012 at 08:43 UTC | |
| |
|
Use hashes instead of grep
by space_monk (Chaplain) on Nov 15, 2012 at 09:05 UTC | |
by ColonelPanic (Friar) on Nov 15, 2012 at 10:22 UTC | |
by space_monk (Chaplain) on Nov 15, 2012 at 10:55 UTC | |
by ColonelPanic (Friar) on Nov 15, 2012 at 11:15 UTC | |
by Anonymous Monk on Nov 15, 2012 at 14:18 UTC | |
by space_monk (Chaplain) on Nov 15, 2012 at 14:01 UTC | |
|
Re: How to grep exact string
by frozenwithjoy (Priest) on Nov 15, 2012 at 08:25 UTC | |
|
Re: How to grep exact string
by space_monk (Chaplain) on Nov 15, 2012 at 13:44 UTC |