Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

removing the last character

Status
Not open for further replies.

99mel

Programmer
Oct 18, 1999
379
GB
I'm looping through a file (each line) and i'm wanting to take out the last '|'. Just wondering whats the quickest way as the file can be over a million lines.

for example:

fielda|fieldb|fieldc|


I want:

fiedla|fieldb|fieldc
 
Try something like this:

my $infile = 'infile.txt';
my $outfile = 'outfile.txt';
[tt]
open (INFILE, $infile) or die "Couldn't open $infile : $!\n";
open (OUTFILE, ">$outfile") or die "Couldn't open $outfile : $!\n";
while (<INFILE>) {
#remove any newline charachters
chomp;
#remove the last charachter
chop;
print OUTFILE $_;
}
close (INFILE);
close (OUTFILE);
[/tt]

Will.
fortytwo
will@hellacool.co.uk
 
or if the &quot;|&quot; char is sometimes there & sometimes not there...
Code:
while (my $line = <INFILE>){
    chomp $line;
    $line =~ s/\|$//; #replaces chop $line;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top