Thank you for your answer, but I think I have expressed myself wrong so I will post my entire code If you can help me. I want to open a file, read the values between the double quotes and replace some numbers from this file with their corresponding value from the first line. This is my code:
#!/usr/bin/perl
# matchtest2.plx
use warnings;
use strict;
open (my $original, '<', 'data.txt') or die $!;
open (my $fh, '>', 'output.txt') or die $!;
while (my $line = <$original>) {
if ($line =~ m/\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\",
+\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(
+.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\"/) {
my $replace = $1;
$line =~ s/[^.]00/$replace/g;
}
if ($line =~ m/\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\",
+\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(
+.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\"/g) {
my $replace1 = $2;
$line =~ s/01/$replace1/g;
}
if ($line =~ m/\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\",
+\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(
+.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\"/g) {
my $replace2 = $3;
$line =~ s/02/$replace2/g;
}
if ($line =~ m/\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\",
+\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(
+.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\"/g) {
my $replace3 = $4;
$line =~ s/03/$replace3/g;
}
if ($line =~ m/\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\",
+\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(
+.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\"/g) {
my $replace4 = $5;
$line =~ s/04/$replace4/g;
}
if ($line =~ m/\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\",
+\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(
+.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\"/g) {
my $replace5 = $6;
$line =~ s/05/$replace5/g;
}
if ($line =~ m/\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\",
+\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(
+.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\"/g) {
my $replace6 = $7;
$line =~ s/06/$replace6/g;
}
if ($line =~ m/\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\",
+\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(
+.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\"/g) {
my $replace7 = $8;
$line =~ s/07/$replace7/g;
}
if ($line =~ m/\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\",
+\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(
+.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\"/g) {
my $replace8 = $9;
$line =~ s/08/$replace8/g;
}
if ($line =~ m/\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\",
+\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(
+.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\"/g) {
my $replace9 = $10;
$line =~ s/09/$replace9 /g;
}
if ($line =~ m/\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\",
+\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(
+.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\"/g) {
my $replace10 = $11;
$line =~ s/[^.]10/$replace10/g;
}
if ($line =~ m/\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\",
+\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(
+.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\"/g) {
my $replace11 = $12;
$line =~ s/11/$replace11/g;
}
if ($line =~ m/\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\",
+\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(
+.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\"/g) {
my $replace12 = $13;
$line =~ s/12/$replace12 /g;
}
if ($line =~ m/\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\",
+\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(
+.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\"/g) {
my $replace13 = $14;
$line =~ s/13/$replace13/g;
}
if ($line =~ m/\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\",
+\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(
+.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\"/g) {
my $replace14 = $15;
$line =~ s/14/$replace14/g;
}
if ($line =~ m/\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\",
+\"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(
+.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\", \"(.*?)\"/g) {
my $replace15 = $16;
$line =~ s/15/$replace15/g;
}
print {$fh} $line;
}
close $original;
close $fh;
and this is my original file(just a short version, the actual one has thousands of numbers) "", "2.90", "3.00", "3.10", "3.20", "3.30", "3.40", "3.50", "3.60", "3
+.70", "3.80", "3.90", "4.00", "4.10", "4.20", "5v"
09 12 12 10 08 08 10 07 10 09 08 08
+ 06 10 10 08 09 11 08 10 07 10 11
00 00 10 12 13 10 09 08 08 06 10 01
+ 08 09 11 08 10 07 10 11 12 13 05
I have to replace for example 00 with the value between the first "" so 00 would be empty string, 01 with the value of the second "2.90" so 01 would become 2.9 and so on... Thank you very much for all your help |