Instead of deleting the unwanted part, you could split each line into parts and just use the wanted part.
#! /usr/bin/perl
use strict;
use warnings;
my $in_file = 'input.txt';
my $out_file = 'output.txt';
my @result;
open my $handle_in, '<', $in_file or die "$in_file: $!";
while ( <$handle_in> ) {
# split (into 2 parts) at = (enclosed in optional whitespaces)
# use the last part and store it in @result
push @result, ( split( /\s*=\s*/, $_, 2 ) )[-1];
}
close $handle_in;
# remove line breaks from array elements
chomp @result;
open my $handle_out, '>', $out_file or die "$out_file: $!";
# print elements (ignore undefined and empty ones)
print $handle_out join( '/', grep { defined && length > 0 } @result )
or die "$out_file: $!";
close $handle_out or die "$out_file: $!";
__END__
Considering ww's message you might need to add more checks and logic to match more complicated input files.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|