Delete text after a known position in a text file

O

Oliver S?der

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?
 
P

Paul Lalli

Oliver S?der said:
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
 
J

Jay Tilton

: : > 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. :)
 
J

Jürgen Exner

Oliver said:
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

Jürgen Exner

Oliver said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top