----- Original Message -----
From: ""Remy Guo"" <[EMAIL PROTECTED]>
Newsgroups: perl.beginners
To: "Perl Beginners" <beginners@perl.org>
Sent: Tuesday, August 05, 2008 10:14 PM
Subject: question about text operation using regex.
hi all,
i have a txt log file and i want to delete the 9th character of each
line.
how can i do it using regex?...thanks!
-Remy
Ooops. Sent it to the poster by mistake.
You could do it from the command line
perl -pe "8 < length && substr $_,8, 1, ''" logfile.txt >
altered_file.txt
Chris
Just to clarify the substr function here. Following the 1, are 2 *single*
quotes. This assigns the empty dtring to the 9th position of the string,
effectively eliminating the 9th character.
Chris
thanks Chris...but is there any solution that i can put in a script?
Remy
Yes Remy, (but please keep your replies to the group and not me so everyone
can see and learn!)
open my $log '.<', 'somefile.log' or die "Unable to open file. $!";
while (<$log>) {
substr $_,8, 1, ''; (or you could use regex approach, s/^(.{8})./$1/)
. . .
. . . do whatever, print, etc.
}
You may use either of the two solutions, (substr or regex). However, when
you use a regex, perl has to start up the regex engine, and there is some
overhead, (i.e. time) involved.
For the record, my original answer was off by one.
perl -pe "8 < length && substr $_,8, 1, ''" logfile.txt > altered_file.txt
that should be:
perl -pe "9 < length && substr $_,8, 1, ''" logfile.txt > altered_file.txt
That 9 accounts for the newline when taking the length of the line.
Chris
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/