Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Help: How to delete the first character from a line?

Reply
Thread Tools

Help: How to delete the first character from a line?

 
 
Amy Lee
Guest
Posts: n/a
 
      08-16-2007
Hello,

There's a file like this,

;348577;aaaaaaaaaaaaaaaaa;3454353453;asdssssssssss ss;234323;asdassssssss
dadssdasef;ttgfhdfg

I hope it can be this,

348577;aaaaaaaaaaaaaaaaa;3454353453;asdsssssssssss s;234323;asdassssssss
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

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      08-16-2007
On Aug 16, 8:48 am, Amy Lee <openlinuxsou...@gmail.com> wrote:
> There's a file like this,
>
> ;348577;aaaaaaaaaaaaaaaaa;3454353453;asdssssssssss ss;234323;asdassssssss
> dadssdasef;ttgfhdfg
>
> I hope it can be this,
>
> 348577;aaaaaaaaaaaaaaaaa;3454353453;asdsssssssssss s;234323;asdassssssss
> 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

 
Reply With Quote
 
 
 
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      08-16-2007
Amy Lee <> wrote in comp.lang.perl.misc:
> Hello,
>
> There's a file like this,
>
> ;348577;aaaaaaaaaaaaaaaaa;3454353453;asdssssssssss ss;234323;asdassssssss
> dadssdasef;ttgfhdfg
>
> I hope it can be this,
>
> 348577;aaaaaaaaaaaaaaaaa;3454353453;asdsssssssssss s;234323;asdassssssss
> 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
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      08-16-2007
On Aug 16, 9:04 am, Paul Lalli <mri...@gmail.com> wrote:
> On Aug 16, 8:48 am, Amy Lee <openlinuxsou...@gmail.com> wrote:
>
> > There's a file like this,

>
> > ;348577;aaaaaaaaaaaaaaaaa;3454353453;asdssssssssss ss;234323;asdassssssss
> > dadssdasef;ttgfhdfg

>
> > I hope it can be this,

>
> > 348577;aaaaaaaaaaaaaaaaa;3454353453;asdsssssssssss s;234323;asdassssssss
> > 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

 
Reply With Quote
 
Amy Lee
Guest
Posts: n/a
 
      08-16-2007
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;asdssssssssss ss;234323;asdassssssss
>> dadssdasef;ttgfhdfg
>>
>> I hope it can be this,
>>
>> 348577;aaaaaaaaaaaaaaaaa;3454353453;asdsssssssssss s;234323;asdassssssss
>> 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
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      08-16-2007
On Aug 16, 9:48 am, Amy Lee <openlinuxsou...@gmail.com> wrote:
> Thank you sir, and $. means amount of lines?


perldoc perlvar
<snip>
$. Current line number for the last filehandle
accessed.


Paul Lalli

 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      08-16-2007
Amy Lee wrote:
> There's a file like this,
>
> ;348577;aaaaaaaaaaaaaaaaa;3454353453;asdssssssssss ss;234323;asdassssssss
> dadssdasef;ttgfhdfg
>
> I hope it can be this,
>
> 348577;aaaaaaaaaaaaaaaaa;3454353453;asdsssssssssss s;234323;asdassssssss
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Open file, get first line, delete first line close file Richard Schneeman Ruby 16 08-26-2008 11:54 PM
character encoding +missing character sequence raavi Java 2 03-02-2006 05:01 AM
getting the character code of a character in a string Velvet ASP .Net 9 01-19-2006 09:27 PM
Character reference "&#c" is an invalid XML character cgbusch XML 6 09-02-2003 07:04 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57