replace string contaning \n

C

cieux87-fin

hello.

I try to change the string \nE by # E without success.

$line=~ s/(\n\E/#E/g;

Thank you in advance for your assistance.
 
S

sheinrich

hello.

I try to change the string \nE by # E without success.

$line=~ s/(\n\E/#E/g;

Thank you in advance for your assistance.

Is it the string constant '\nE' that you'd like to replace or do you
want to replace all line breaks by '#' which are being followed by
'E'?
And what does the '(' in your regex stand for?

In the first case
$line=~ s/\\nE/#E/g;

in the 2nd case
$line=~ s/\nE/#E/g;

Cheers,
Steffen
 
T

Tad McClellan

I try to change the string \nE by # E without success.

$line=~ s/(\n\E/#E/g;


That does not even compile.

Two pieces of information are needed to evaluate the behavior
of a pattern match, the pattern and the string that it is
trying to match against.

We need to see the contents of $line.


Your choice of variable name implies that the answer is
given in the Perl FAQ:

perldoc -q match

I'm having trouble matching over more than one line. What's wrong?
 
C

cieux87-fin

Is it the string constant '\nE' that you'd like to replace or do you
want to replace all line breaks by '#' which are being followed by
'E'?
And what does the '(' in your regex stand for?

In the first case
$line=~ s/\\nE/#E/g;

in the 2nd case
$line=~ s/\nE/#E/g;

Cheers,
Steffen

Hello

Here a solution.

# replace all \n by space
sub chg_file {
# Open file to modify for reading
open(INPUTF,$ARGV[0]);

while(<INPUTF>) {
if (m/^E /) {
print OUTFILE "\n" unless $lin < 2;
}
print OUTFILE " " unless m/^E /;
chop $_;
print OUTFILE $_;
$lin++;
}
print OUTFILE "\n";
close(OUTFILE);
close(INPUTF);
}
 
C

cieux87-fin

That does not even compile.

Two pieces of information are needed to evaluate the behavior
of a pattern match, the pattern and the string that it is
trying to match against.

We need to see the contents of $line.

Your choice of variable name implies that the answer is
given in the Perl FAQ:

perldoc -q match

I'm having trouble matching over more than one line. What's wrong?

hello

Here a solution.

# replace all \n by space
sub chg_file {
# Open file to modify for reading
open(INPUTF,$ARGV[0]);

while(<INPUTF>) {
if (m/^E /) {
print OUTFILE "\n" unless $lin < 2;
}
print OUTFILE " " unless m/^E /;
chop $_;
print OUTFILE $_;
$lin++;
}
print OUTFILE "\n";
close(OUTFILE);
close(INPUTF);
}
 
T

Tad McClellan

hello

Here a solution.


I did not ask a question...

sub chg_file {
# Open file to modify for reading
open(INPUTF,$ARGV[0]);


You should always, yes *always*, check the return value from open():

open(INPUTF, $ARGV[0]) or die "could not open '$ARGV[0]' $!";

if (m/^E /) {
print OUTFILE "\n" unless $lin < 2;
}
print OUTFILE " " unless m/^E /;


if (m/^E /) {
print OUTFILE "\n" unless $. < 2;
}
else {
print OUTFILE " ";
}



That is how you removed newlines 10 years ago. Where did you
learn your Perl?


chomp $_;



You don't need to maintain a line counter yourself, perl is already
doing that for you, just use it ($.).
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top