0

I am trying to run this command git status -vv | awk 'NR>5 {print $0}' from within a Python file. But I cannot get the awk command working.

Here is a sample result of my git st:

# On branch master
# Your branch is ahead of master by 2 commits.
#
#
#       modified:   file1
#       modified:   file2
#       modified:   file3

When I run the command from the terminal, I get what I want:

#       modified:   file1
#       modified:   file2
#       modified:   file3

I am having trouble implementing it from within a Python script:

import sys
import subprocess as sb

ps = sb.Popen(("git","status","-vv"),stdout=sb.PIPE)
output = sb.check_output(('awk','"NR>5 {print $0}"'),stdin=ps.stdout)
print output

However, this returns just the git st results without the awk performed on the lines. How can I do this from within python to get the same output as I do when running in the terminal.

2 Answers 2

2

The following code should work (just remove the double quotes for awk's argument)

import sys
import subprocess as sb

ps = sb.Popen(("git","status","-vv"),stdout=sb.PIPE)
output = sb.check_output(('awk','NR>5 {print $0}'),stdin=ps.stdout)
print output
Sign up to request clarification or add additional context in comments.

2 Comments

This worked perfectly. And kudos for actually looking at my provided code and catching a fix instead of just recommending to use shell=True !
I think for awk subprocess.check_ouput(...) is the correct way to run this command for shell= False. This worked for me too
0

This might be much simpler:

#!/usr/bin/python3                                                                                                                                                                 

import sys
import subprocess as sb

cmd = "git status -vv | awk '(NR>5){ print $0 }'"

output = sb.check_output(cmd, stderr=sb.STDOUT, shell=True)
sys.stdout.write('{}'.format(output))

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.