Help: How to delete the first character from a line?
Help: How to delete the first character from a line?
am 16.08.2007 14:48:03 von Amy Lee
Hello,
There's a file like this,
;348577;aaaaaaaaaaaaaaaaa;3454353453;asdssssssssssss;234323; asdassssssss
dadssdasef;ttgfhdfg
I hope it can be this,
348577;aaaaaaaaaaaaaaaaa;3454353453;asdssssssssssss;234323;a sdassssssss
dadssdasef;ttgfhdfg
So my problem is how use Perl to delete the first character of the first
line. Use \s?
Thank you very much~
Regards,
Amy Lee
Re: Help: How to delete the first character from a line?
am 16.08.2007 15:04:27 von Paul Lalli
On Aug 16, 8:48 am, Amy Lee wrote:
> There's a file like this,
>
> ;348577;aaaaaaaaaaaaaaaaa;3454353453;asdssssssssssss;234323; asdassssssss
> dadssdasef;ttgfhdfg
>
> I hope it can be this,
>
> 348577;aaaaaaaaaaaaaaaaa;3454353453;asdssssssssssss;234323;a sdassssssss
> dadssdasef;ttgfhdfg
>
> So my problem is how use Perl to delete the first character of the first
> line. Use \s?
I have no idea how the space metacharacter would help you here.
I would just use four-arg substr.
perl -lpi.bkp -e'substr($_, 0, 1, "");' file.txt
perldoc -f substr
There Is, of course, More Than One Way To Do It. Other ways include:
search-and-replace: s/^.//;
reverse/chomp: $_ = reverse $_; chomp; $_ = reverse $_;
three-arg substr: substr($_, 0, 1) = "";
If one of those makes more sense to you than the others, go for it. I
prefer the substr.
Paul Lalli
Re: Help: How to delete the first character from a line?
am 16.08.2007 15:14:25 von anno4000
Amy Lee wrote in comp.lang.perl.misc:
> Hello,
>
> There's a file like this,
>
> ;348577;aaaaaaaaaaaaaaaaa;3454353453;asdssssssssssss;234323; asdassssssss
> dadssdasef;ttgfhdfg
>
> I hope it can be this,
>
> 348577;aaaaaaaaaaaaaaaaa;3454353453;asdssssssssssss;234323;a sdassssssss
> dadssdasef;ttgfhdfg
>
> So my problem is how use Perl to delete the first character of the first
> line. Use \s?
Your question is not very clear. Do you want to change the disk file
so that the leading semicolon is no longer there? Or do you want to
keep it in the file, but remove it from the line before processing the
line further?
What do you mean with "use \s"? "\s" denotes white space in a regular
expression, but that won't help deleting a semicolon.
Assuming you want to keep the file as it is, and assuming that $fh is
a readable file handle to your file (untested):
while ( <$fh> ) {
s/^;// if $. == 1; # only on the first line
# further processing
}
Anno
Re: Help: How to delete the first character from a line?
am 16.08.2007 15:16:52 von Paul Lalli
On Aug 16, 9:04 am, Paul Lalli wrote:
> On Aug 16, 8:48 am, Amy Lee wrote:
>
> > There's a file like this,
>
> > ;348577;aaaaaaaaaaaaaaaaa;3454353453;asdssssssssssss;234323; asdassssssss
> > dadssdasef;ttgfhdfg
>
> > I hope it can be this,
>
> > 348577;aaaaaaaaaaaaaaaaa;3454353453;asdssssssssssss;234323;a sdassssssss
> > dadssdasef;ttgfhdfg
>
> > So my problem is how use Perl to delete the first character of the first
> > line.
Hrm. I didn't register the "of the first line" part of this when I
answered. My answer was presuming you wanted to delete the first
character of each line.
> I would just use four-arg substr.
>
> perl -lpi.bkp -e'substr($_, 0, 1, "");' file.txt
Simply add a check in there to make sure you only do this to the first
line:
perl -lpi.bkp -e'substr($_, 0, 1, "") if $. == 0;' file.txt
or, slurp the entire file at once rather than reading/writing line-by-
line (not recommended for large files):
perl -0 0777 -pi.bkp -e'substr($_, 0, 1, "");' file.txt
Paul Lalli
Re: Help: How to delete the first character from a line?
am 16.08.2007 15:48:38 von Amy Lee
On Thu, 16 Aug 2007 13:14:25 +0000, anno4000 wrote:
> Amy Lee wrote in comp.lang.perl.misc:
>> Hello,
>>
>> There's a file like this,
>>
>> ;348577;aaaaaaaaaaaaaaaaa;3454353453;asdssssssssssss;234323; asdassssssss
>> dadssdasef;ttgfhdfg
>>
>> I hope it can be this,
>>
>> 348577;aaaaaaaaaaaaaaaaa;3454353453;asdssssssssssss;234323;a sdassssssss
>> dadssdasef;ttgfhdfg
>>
>> So my problem is how use Perl to delete the first character of the first
>> line. Use \s?
>
> Your question is not very clear. Do you want to change the disk file
> so that the leading semicolon is no longer there? Or do you want to
> keep it in the file, but remove it from the line before processing the
> line further?
>
> What do you mean with "use \s"? "\s" denotes white space in a regular
> expression, but that won't help deleting a semicolon.
>
> Assuming you want to keep the file as it is, and assuming that $fh is
> a readable file handle to your file (untested):
>
> while ( <$fh> ) {
> s/^;// if $. == 1; # only on the first line
> # further processing
> }
>
> Anno
Thank you sir, and $. means amount of lines?
Regards,
Amy Lee
Re: Help: How to delete the first character from a line?
am 16.08.2007 16:48:54 von Paul Lalli
On Aug 16, 9:48 am, Amy Lee wrote:
> Thank you sir, and $. means amount of lines?
perldoc perlvar
$. Current line number for the last filehandle
accessed.
Paul Lalli
Re: Help: How to delete the first character from a line?
am 16.08.2007 17:32:52 von Gunnar Hjalmarsson
Amy Lee wrote:
> There's a file like this,
>
> ;348577;aaaaaaaaaaaaaaaaa;3454353453;asdssssssssssss;234323; asdassssssss
> dadssdasef;ttgfhdfg
>
> I hope it can be this,
>
> 348577;aaaaaaaaaaaaaaaaa;3454353453;asdssssssssssss;234323;a sdassssssss
> dadssdasef;ttgfhdfg
>
> So my problem is how use Perl to delete the first character of the first
> line.
open my $file, '+<', 'myfile.txt' or die $!;
my @lines = <$file>;
$lines[0] = substr $lines[0], 1;
seek $file, 0, 0;
truncate $file, 0;
print $file @lines;
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl