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

Discussion in 'Perl Misc' started by Amy Lee, Aug 16, 2007.

  1. Amy Lee

    Amy Lee Guest

    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;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
     
    Amy Lee, Aug 16, 2007
    #1
    1. Advertising

  2. Amy Lee

    Paul Lalli Guest

    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;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
     
    Paul Lalli, Aug 16, 2007
    #2
    1. Advertising

  3. Amy Lee

    -berlin.de Guest

    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;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
     
    -berlin.de, Aug 16, 2007
    #3
  4. Amy Lee

    Paul Lalli Guest

    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;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
     
    Paul Lalli, Aug 16, 2007
    #4
  5. Amy Lee

    Amy Lee Guest

    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;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
     
    Amy Lee, Aug 16, 2007
    #5
  6. Amy Lee

    Paul Lalli Guest

    On Aug 16, 9:48 am, Amy Lee <> wrote:
    > Thank you sir, and $. means amount of lines?


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


    Paul Lalli
     
    Paul Lalli, Aug 16, 2007
    #6
  7. 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;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
     
    Gunnar Hjalmarsson, Aug 16, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tor Erik Sønvisen

    Delete first line from file

    Tor Erik Sønvisen, Mar 1, 2005, in forum: Python
    Replies:
    4
    Views:
    7,499
    Jeff Sandys
    Mar 4, 2005
  2. Alex Stapleton

    RE: Delete first line from file

    Alex Stapleton, Mar 1, 2005, in forum: Python
    Replies:
    0
    Views:
    521
    Alex Stapleton
    Mar 1, 2005
  3. Juergen Huber

    delete first line in a file

    Juergen Huber, Jun 30, 2006, in forum: Python
    Replies:
    10
    Views:
    1,048
    Dennis Lee Bieber
    Jun 30, 2006
  4. Dong Ge

    How to delete a line or a character?

    Dong Ge, Oct 4, 2004, in forum: C Programming
    Replies:
    8
    Views:
    345
    Jason Curl
    Oct 5, 2004
  5. Richard Schneeman
    Replies:
    16
    Views:
    561
    Daniel Bush
    Aug 27, 2008
Loading...

Share This Page