Delete text after a known position in a text file

Discussion in 'Perl Misc' started by Oliver S?der, Sep 17, 2004.

  1. Oliver S?der

    Oliver S?der Guest

    I want to delete all text after the word "lib\" in a line of a text
    field.
    IDMLIB=C:\PROGRA~1\Novadigm\Lib\

    How can I make $_ all text behind the last back slash? Then I would be
    able to do s/$_//. Or is there a better way?
     
    Oliver S?der, Sep 17, 2004
    #1
    1. Advertising

  2. Oliver S?der

    Paul Lalli Guest

    "Oliver S?der" <> wrote in message
    news:...
    > I want to delete all text after the word "lib\" in a line of a text
    > field.
    > IDMLIB=C:\PROGRA~1\Novadigm\Lib\
    >
    > How can I make $_ all text behind the last back slash? Then I would be
    > able to do s/$_//. Or is there a better way?


    my $string = 'IDMLIB=C:\PROGRA~1\Novadigm\Lib\';
    $string =~ s/(lib\\).*/$1/i;

    Simply search for the marker, and replace the marker and everything that
    follows with just the marker.

    Paul Lalli
     
    Paul Lalli, Sep 17, 2004
    #2
    1. Advertising

  3. Oliver S?der

    Jay Tilton Guest

    "Paul Lalli" <> wrote:

    : "Oliver S?der" <> wrote in message
    : news:...
    : > I want to delete all text after the word "lib\" in a line of a text
    : > field.
    : > IDMLIB=C:\PROGRA~1\Novadigm\Lib\
    : >
    : > How can I make $_ all text behind the last back slash? Then I would be
    : > able to do s/$_//. Or is there a better way?
    :
    : my $string = 'IDMLIB=C:\PROGRA~1\Novadigm\Lib\';
    : $string =~ s/(lib\\).*/$1/i;
    :
    : Simply search for the marker, and replace the marker and everything that
    : follows with just the marker.

    Or, if you want to be weird:

    #!perl
    use warnings;
    use strict;

    $_ = 'IDMLIB=C:\PROGRA~1\Novadigm\Lib\blah\blah';
    {
    open my($f), '<', \$_ or die $!;
    local $/ = 'Lib\\';
    $_ = <$f>;
    }
    print;
    __END__

    Don't try this at work, kids. :)
     
    Jay Tilton, Sep 18, 2004
    #3
  4. Oliver S?der wrote:
    > I want to delete all text after the word "lib\" in a line of a text
    > field.
    > IDMLIB=C:\PROGRA~1\Novadigm\Lib\
    >
    > How can I make $_ all text behind the last back slash? Then I would be
    > able to do s/$_//. Or is there a better way?


    What's wrong with a simple
    s/lib\\.*/lib\\/i;
    Replace "lib\" and anything after it with "lib\" and ignore the case, too.

    jue
     
    Jürgen Exner, Sep 18, 2004
    #4
  5. Oliver S?der wrote:
    > I want to delete all text after the word "lib\" in a line of a text
    > field.
    > IDMLIB=C:\PROGRA~1\Novadigm\Lib\
    >
    > How can I make $_ all text behind the last back slash? Then I would be
    > able to do s/$_//. Or is there a better way?


    Another way (considering that according to your subject line you already
    know the position of the text) is to use substr:
    substr ($text, $KnownPosition + length('lib\')) = '';

    jue
     
    Jürgen Exner, Sep 18, 2004
    #5
    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. LisaBigJax
    Replies:
    5
    Views:
    24,688
    tommynzuk
    Oct 23, 2006
  2. da Vinci
    Replies:
    23
    Views:
    11,874
    Robbie Hatley
    Jun 28, 2004
  3. Replies:
    3
    Views:
    8,494
  4. Raj
    Replies:
    13
    Views:
    1,657
  5. optimistx

    scrolling to a known text?

    optimistx, Feb 16, 2004, in forum: Javascript
    Replies:
    0
    Views:
    101
    optimistx
    Feb 16, 2004
Loading...

Share This Page