I
IcyMint
Hi, I'm trying to change the contents of a file base on the substitute
list from the other file. I'd copied my progress over here, albeit with
some changes. The content file format is: #name group OR #name \n+
group
The script is working fine, but I want it to compare if the group name
is correct. If it is incorrect, change it, and count how many times it
had been changed. What I'm trying to do is something like:
while ($content =~ /\n\#(\S+)\b\s+(\S+)/g) {
my($name,$group) = ($1,$2);
unless ($group eq $data{$name}) {
$2 = $data{$name};
$count{$name}++;
}
}
It's not a valid code, it gave me errors, but i'm just including it so
that maybe you'll understand what I'm trying to achieve. Can you help
me with this? Thank you!
open(FILE,"content.txt") or die "Cannot open file content.txt~ $!\n";
open(DATA,"data.txt") or die "Cannot open file data.txt~ $!\n";
undef $/;
my $content = <FILE>;
my $data = <DATA>;
while ($data =~ /\nsubstitute\s+(\S+)\s+(\S+)/g) {
$data{$1} = $2;
}
foreach my $key (keys %data) {
$content =~ s/\n\#$key\b\s*\S+/\n\#$key $data{$key}/g;
$content =~ s/\n\#$key\b\s*\n\+\s+\S+/\n\#$key\n\+\s$data{$key}/g;
}
The content.txt(modified version) file looks something like this:
#benjamin TB3
#desmond TG2
#terrence TE1
#abigail_lim_suet_ching
+ KR8
list from the other file. I'd copied my progress over here, albeit with
some changes. The content file format is: #name group OR #name \n+
group
The script is working fine, but I want it to compare if the group name
is correct. If it is incorrect, change it, and count how many times it
had been changed. What I'm trying to do is something like:
while ($content =~ /\n\#(\S+)\b\s+(\S+)/g) {
my($name,$group) = ($1,$2);
unless ($group eq $data{$name}) {
$2 = $data{$name};
$count{$name}++;
}
}
It's not a valid code, it gave me errors, but i'm just including it so
that maybe you'll understand what I'm trying to achieve. Can you help
me with this? Thank you!
open(FILE,"content.txt") or die "Cannot open file content.txt~ $!\n";
open(DATA,"data.txt") or die "Cannot open file data.txt~ $!\n";
undef $/;
my $content = <FILE>;
my $data = <DATA>;
while ($data =~ /\nsubstitute\s+(\S+)\s+(\S+)/g) {
$data{$1} = $2;
}
foreach my $key (keys %data) {
$content =~ s/\n\#$key\b\s*\S+/\n\#$key $data{$key}/g;
$content =~ s/\n\#$key\b\s*\n\+\s+\S+/\n\#$key\n\+\s$data{$key}/g;
}
The content.txt(modified version) file looks something like this:
#benjamin TB3
#desmond TG2
#terrence TE1
#abigail_lim_suet_ching
+ KR8