I want to pipe the output of ps -ef
to python line by line.
The script I am using is this (first.py) -
#! /usr/bin/python
import sys
for line in sys.argv:
print line
Unfortunately, the "line" is split into words separated by whitespace. So, for example, if I do
echo "days go by and still" | xargs first.py
the output I get is
./first.py
days
go
by
and
still
How to write the script such that the output is
./first.py
days go by and still
?
Instead of using command line arguments I suggest reading from standard input (stdin
). Python has a simple idiom for iterating over lines at stdin
:
import sys
for line in sys.stdin:
sys.stdout.write(line)
My usage example (with above's code saved to iterate-stdin.py
):
$ echo -e "first line\nsecond line" | python iterate-stdin.py
first line
second line
With your example:
$ echo "days go by and still" | python iterate-stdin.py
days go by and still