1

This is related to this question

Where combination of all lines that end with a backslash character is done. The following Perl command works in terminal but not in Perl script:

perl -i -p -e 's/\\\n//' input_file

In Perl script, I set it this way but it does not work:

`perl -i -p -e 's/\\\n//' $input_file_path_variable`;

**Updated the file content and code used

Input file content as follows:

foo bar \
bash \
baz
dude \
happy

Desired output content:

foo bar bash baz
dude happy

Current script:

#!/usr/bin/env perl 

use Getopt::Long;
use FindBin;
use File::Path;
use File::Copy;
use DirHandle;
use FileHandle;
use File::Basename;
use Env;
use File::Slurp;

my $dummy_file = "/wdir/dummy.txt";
my $file_content = read_file($dummy_file);
print "$file_content\n";
print "==============================================\n"

my $ttest = $file_content =~ s/\\\n//;
print "$ttest\n";

Current Output

foo bar \
bash \
baz
dude \
happy
==============================================
1

8
  • Backticks act as double-quoted string literals, and \\ becomes \ in double-quoted string literals. /// Also, you have a code injection bug. That's not the proper way to create a shell command. See String::ShellQuote. /// Finally, why are you using backticks for a program that produces no output to STDOUT? You could avoid having to use String::ShellQuote by using the multi-argument form of system. Commented Oct 27, 2020 at 8:11
  • @tobyink Yes, may I know the correct way to do this? I tried reading the whole file into a variable and applied Oliver's answer but unable to get desired output. Commented Oct 27, 2020 at 8:17
  • 1
    Show us your code, the input you are using, and a demonstration of how it is not working. Commented Oct 27, 2020 at 8:26
  • 1
    @TLP My bad on that, I have updated the question with the input used. You are right on your assumption and your answer fixed my problem. Thank you. Commented Oct 27, 2020 at 8:52
  • @tobyink Point taken on not using a Perl one liner in backticks within another Perl script, I have no clue that this is a bad idea. Thought this is the fastest way to do what I want and change the content of the input file. Commented Oct 27, 2020 at 9:01

3 Answers 3

3

I think I realized what your problem is. You need to make your substitution match multiple times.

You are working with this one-liner:

perl -i -p -e 's/\\\n//' input_file

Which will replace the backslash and newline once per line in a file read in line-by-line mode. And now you are trying to implement it inside another program, where you are slurping a whole file into a variable, as mentioned in your comment on another answer:

I tried reading the whole file into a variable and applied your solution, not working for me: my $file_content = read_file($input_file_path_variable) and then $file_content =~ s/\\n//;

This will only replace one time. The first match in the file. You need to add the modifier /g to make the match global, as many times as possible:

$file_content =~ s/\\\n//g;

I am not sure how you are using your code, since you are not showing us. This makes answering simple questions hard. But if my assumptions are correct, this will fix your problem.

Sign up to request clarification or add additional context in comments.

Comments

2

I think you've changed your code/approach at least once since originally asking the question, but based on your current code, your issue is this line:

my $ttest = $file_content =~ s/\\\n//;

Firstly, the s/// operator needs a g at the end so that it's a global search and replace, instead of stopping after the first substitution.

Secondly, $file_content =~ s/\\\n// doesn't return the final string; it modifies the string in-place. (So it's changing $file_content.) In recent versions of Perl, you can add an r modifier to the end to get it to return the modified string.

So either of these will work for you:

my $ttest = ( $file_content =~ s/\\\n//gr );

Or:

( my $ttest = $file_content ) =~ s/\\\n//g;

Full script which produces the output you want:

#!/usr/bin/env perl 

use strict;
use warnings;
use File::Slurp;

my $file_content = read_file('/tmp/dummy.txt');

(my $ttest = $file_content) =~ s/\\\n//g;

print "$file_content\n";
print "==============================================\n";
print "$ttest\n";

Comments

1

In a Perl you should write:

$input_file_variable =~ s/\\\n//;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.