1

I am trying to run the following command in perl script :

#!/usr/bin/perl

my $cmd3 =`sed ':cycle s/^\(\([^,]*,\)\{0,13\}[^,|]*\)|[^,]*/\1/;t cycle' file1 >file2`;
 system($cmd3);

but is not producing any output nor any error. Although when I am running the command from command line it is working perfectly and gives desired output. Can you guys please help what I am doing wrong here ? Thanks

Vieypul
  • 29
  • 4
  • 3
    Perl is a superset of `sed` so it doesn't make any sense to involve `sed` at all. `perl -pe '1 while s/^(([^,]*,){0,13}[^,|]*)\|[^,]*/$1/' file1 >file2` – tripleee Apr 16 '15 at 12:14
  • @tripleee Thanks, I will take your advice on it. I didn't knew that Perl is a superset of sed . It worded for me but is there any option in this like we have in sed "i" to write the output back to the same file. Thanks again ! – Vieypul Apr 17 '15 at 03:45
  • @tripleee Also this perl command is not working into the perl sccript. but I can use this on command line. What should I modify to run in inside a perl script ? – Vieypul Apr 17 '15 at 05:20
  • Perl `-i` is available just like (sometimes) `sed -i`; but then of course the redirection to `file2` doesn't make any sense. – tripleee Apr 17 '15 at 06:18
  • @tripleee Thanks but this command is not working inside perl script. how can I achieve this ? – Vieypul Apr 17 '15 at 06:27

3 Answers3

1

system() doesn't return the output, just the exit status.

Juan Diego Godoy Robles
  • 14,447
  • 2
  • 38
  • 52
1

To see the output, print $cmd3.

my $cmd3 = `sed ':cycle s/^\(\([^,]*,\)\{0,13\}[^,|]*\)|[^,]*/\1/;t cycle' file1 >file2`;
print "$cmd3\n";

Edit:
If you want to check for exceptional return values, use CPAN module IPC::System::Simple:

use IPC::System::Simple qw(capture);

my $result = capture("any-command"); 
serenesat
  • 4,611
  • 10
  • 37
  • 53
0

Running sed from inside Perl is just insane.

#!/usr/bin/perl

open (F, '<', "file1") or die "$O: Could not open file1: $!\n";
while (<F>) {
    1 while s/^(([^,]*,){0,13}[^,|]*)\|[^,]*/$1/;
    print;
}

Notice how Perl differs from your sed regex dialect in that grouping parentheses and alternation are unescaped, whereas a literal round parenthesis or pipe symbol needs to be backslash-escaped (or otherwise made into a literal, such as by putting it in a character class). Also, the right-hand side of the substitution prefers $1 (you will get a warning if you use warnings and have \1 in the substitution; technically, at this level, they are equivalent).

man perlrun has a snippet explaining how to implement the -i option inside a script if you really need that, but it's rather cumbersome. (Search for the first occurrence of "LINE:" which is part of the code you want.)

However, if you want to modify file1 in-place, and you pass it to your Perl script as its sole command-line argument, you can simply say $^I = 1; (or with use English; you can say $INPLACE_EDIT = 1;). See man perlvar.

By the way, the comment that your code "isn't producing any output" isn't entirely correct. It does what you are asking it to; but you are apparently asking for the wrong things.

Quoting a command in backticks executes that command. So

my $cmd3 = `sed ... file1 >file2`;

runs the sed command in a subshell, there and then, with input from file1, and redirected into file2. Because of the redirection, the output from this pipeline is nothing, i.e. an empty string "", which is assigned to $cmd3, which you then completely superfluously attempt to pass to system.

Maybe you wanted to put the sed command in regular quotes instead of backticks (so that the sed command line would be the value of $cmd3, which it then makes sense to pass to system). But because of the redirection, it would still not produce any visible output; it would create file2 containing the (possibly partially substituted) text from file1.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Your question specifically didn't ask about `-i` but it's in a comment. If you need inline replacement as part of a larger script, calling out to an external tool which does it elegantly starts to seem less insane. – tripleee Apr 17 '15 at 07:26