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.

4
  • possible duplicate of How do you capture stderr, stdout, and the exit code all at once, in Perl? Commented Mar 3, 2015 at 12:34
  • Can you give some example of the output you're parsing? Commented Mar 3, 2015 at 12:35
  • @AlessandroDaRugna: The return code and the contents of stderr are being ignored in that Python code Commented Mar 3, 2015 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() Commented Mar 4, 2015 at 9:57

2 Answers 2

2

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

my @split = `$our_cmd`;
chomp @split;
Sign up to request clarification or add additional context in comments.

2 Comments

I wasn't sure about the strip - that looks like it's deleting some characters I think?
@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.
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.

1 Comment

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.

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.