Hi, I have a file that I've overtly replaced certain text with a | to assist in loading the file into a table. As the last field could contain actual |'s as part of a command sequence, I want to replace any |'s after the 9th occurrence with the word pipe to load into a varchar field. It works for the 10th occurrence but somehow skips over the 11th and treats the 12th as the 11th. I.e, "|some command with the 10th | along with the 11th | along with the 12th |" results in "|some command with the 10th pipe along with the 11th | along with the 12th pipe " Is there something inherent with my approach that is unable to accurately determine the 11th occurrence of a |?...a humble novice.
open($fh, '<:encoding(UTF-8)', $outfilename) or die "Could not open fi +le '$outfilename' $!"; # open the file that had 9 |'s overtly set. open(OUT, ">$outfileload") or die "Unable to open $outfilename for wri +ting: $!\n"; # replace the 10th, 11th, and 12th |'s with pipe so the field can be +loaded into an expanded varchar field without treating the data as mo +re than one field. while ($row = <$fh>) { my $pos1 = 10; $row =~ s/(\|)/!--$pos1 ? ' pipe ' : $1/ge; my $pos2 = 11; $row =~ s/(\|)/!--$pos2 ? ' pipe ' : $1/ge; my $pos3 = 12; $row =~ s/(\|)/!--$pos3 ? ' pipe ' : $1/ge; chomp $row; print OUT "$row\n"; } print "doneagain\n";
In reply to replace nth occurrence of | by perl197
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |