0
p = Popen(our_cmd, shell=True, stdout=PIPE, stderr=PIPE)
output = p.communicate()[0]
split = output.strip('\n\t\r').split("\n")

I want to execute this command which is in string our_cmd

what I have tried is this

my @output = `$our_cmd`; ## here command is executing
my @split = grep(s/\s*$//g, @output); ## the format and putting in new array
@split = split("\n", @split);

My command is executing but not taking input properly. I need output in array in format as in Python code.

amphetamachine
  • 27,620
  • 12
  • 60
  • 72
  • possible duplicate of [How do you capture stderr, stdout, and the exit code all at once, in Perl?](http://stackoverflow.com/questions/109124/how-do-you-capture-stderr-stdout-and-the-exit-code-all-at-once-in-perl) – Alessandro Da Rugna Mar 03 '15 at 12:34
  • Can you give some example of the output you're parsing? – Sobrique Mar 03 '15 at 12:35
  • @AlessandroDaRugna: The return code and the contents of `stderr` are being ignored in that Python code – Borodin Mar 03 '15 at 12:54
  • your Python code accumulates `stderr` in memory and then just discards it at the end. To get stdout lines from a shell command as a list and to discard its stderr: `lines = check_output(out_cmd, shell=True, stderr=DEVNULL).splitlines()` – jfs Mar 04 '15 at 09:57

2 Answers2

2

As far as I can tell from your question, all you need is

my @split = `$our_cmd`;
chomp @split;
Borodin
  • 126,100
  • 9
  • 70
  • 144
  • I wasn't sure about the `strip` - that looks like it's deleting some characters I think? – Sobrique Mar 03 '15 at 12:43
  • @Sobrique: It's removing all tabs, newlines, and carriage returns from the beginning and end of the string. Otherwise `split` will return an empty string at the end. – Borodin Mar 03 '15 at 12:44
0

I think you're misunderstanding a couple of perl concepts here. For example - you're spliting an array - which doesn't make a lot of sense, because split turns a string into an array, based on a delimiter.

Likewise grep - that's quite an unusual use of grep because you've got a search and replace pattern embedded - usually grep is for filtering based on some boolean expression. (I suspect it works like that, but I'm not entirely sure if your replacement pattern returns true/false, which'll do odd things in a grep context).

So how about instead:

my @output = `$our_command`;

chomp @output; #removes linefeeds from each element. 

for ( @output ) { s/[\t\r]//g; }; #removes linefeeds and carriage returns

This will put into @output one element per line (including linefeed) and then remove any \t or \r in there. If you don't want the linefeed, as Borodin says - chomp @output; will deal with that.

As mentioned in comments - this may not exactly reproduce what strip is doing, and the strip operation may be irrelevant in perl.

Testing your grep:

my @test =  ( "test aaa bbb", "mooo", " aaa Moo MMOoo", "no thing in it" );
print join ("\n", grep { s/aaa//g } @test );

This does do the search and replace on $_ (each line of the grep), but the replace expression does return a 'true/false' - meaning you effectively discard elements that don't contain the pattern at all.

Sobrique
  • 52,974
  • 7
  • 60
  • 101
  • 1
    The Python code only removes those characters from the beginning of the first line and the end of the last line of the output. There should never be any CR characters anyway. – Borodin Mar 03 '15 at 12:57