I'm at a loss and hoping to find help here. What I'm trying to accomplish is the following: I have a .csv file with 8 columns. The third column contains phone numbers formatted like so:
+45 23455678
+45 12314425
+45 43631678
+45 12345678
(goes on for a while)
What I want is:
+45 2345 5678
+45 1231 4425
+45 4363 1678
+45 1234 5678
(etc)
So just a whitespace after the 8th position (inc the + and whitespace). I've tried various things but it's not working. First I tried it with substr but couldn't get it to work. Then looked at the split function. And then I got confused! I'm new to perl so I'm not sure what I'm looking for but I've tried everything. There's 1 condition, all the numbers begin with (let's say) +45 and then a whitespace and a block of numbers. But not all the numbers have the same length, some have more than 10 digits. What I want it to do is take the first bit "+45 1234" (/+43\s{1}\d{4}/) and then the second part no matter how many digits it has. I figured setting LIMIT to 1 so it just adds the last bit no matter if its 4 digits or 8 long.
I've read http://www.perlmonks.org/?node_id=591988, but the part "Using split versus Regular Expressions" got me confused.
I've been trying for 3 days now and not getting anywhere. I guess it should be simple but I'm just now getting to know the basics of perl. I do have an understanding of regular expression but I don't know what statement to use for a certain task. This is my code:
@ARGV or die "Usage: $0 input-file output-file\n";
$inputfile=$ARGV[0];
$outputfile=$ARGV[1];
open(INFILE,$inputfile) || die "Bestand niet gevonden :$!\n";
open(OUTFILE,">$outputfile") || die "Bestand niet gevonden :$!\n";
$i = 0;
@infile=<INFILE>;
foreach ( @infile ) {
$infile[$i] =~ s/"//g;
@elements = split(/;/,$infile[$i]);
@split = split(/\+43\s{1}\d{4}/, $elements[2], 1);
@split = join ???
@elements = join(";",@elements); # Add ';' to all elements
print OUTFILE "@elements";
$i = $i+1;
}
close(INFILE);
close(OUTFILE);