Suppose I have a python script, which normally would be executed as

python s.py

Instead of having to type data in manually, I'd like to forward a file, text.txt, as an input:

 python s.py < text.txt

Now, this text.txt contains more lines than s.py asks for, because it is supposed to cover several passes: first I'm feeding lines 1-7 of text.txt, then lines 8-14, then 15-21 etc. I'm going to take care of it by using a loop, but I can't figure out how to input only lines 1-7 of text.txt into python s.py. I tried something like

python s.py < sed -n 1,7p txt.txt


python s.py < cat txt.txt | sed -n 1,7p

, but to no avail. Any ideas?

  • why not get the needed slices inside the python script itself? May 23 '18 at 11:20
  • If I have several thousands of lines and, thus, need several hundreds of python s.py executions, I wouldn't want to overcomplicate the s.py by making it aware of which pass we are currently at.
    – Yury
    May 23 '18 at 11:27

Use pipe:

sed -n 1,7p txt.txt | python s.py

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.