Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get output of grep command (Python)

I have an input file test.txt as:

host:dc2000
host:192.168.178.2

I want to get all of addresses of those machines by using:

grep "host:" /root/test.txt 

And so on, I get command ouput via python:

import subprocess
file_input='/root/test.txt'
hosts=subprocess.Popen(['grep','"host:"',file_input], stdout= subprocess.PIPE)
print hosts.stdout.read()

But the result is empty string.

I don't know what problem I got. Can you suggest me how to solve?

like image 969
SieuTruc Avatar asked Oct 09 '12 22:10

SieuTruc


1 Answers

Try that :

import subprocess
hosts = subprocess.check_output("grep 'host:' /root/test.txt", shell=True)
print hosts
like image 94
Gilles Quenot Avatar answered Sep 20 '22 09:09

Gilles Quenot