0

If I am writing some python and I only want the terminal to show a certain line, what command do I use? For example my program is: print("line one") print("line two") print("line three")

And I only want the code in line two to appear in the terminal - print("line two") - The command I'd use to run the whole program is python programname.py but how do I print a specific line?

  • 2
    Please clarify your question. Are you in interactive python shell? Are you writing code in a text editor? Do you want to print a line from a source file according to some pattern? What does python have to do with this question? – orion Jan 26 '15 at 21:56
  • I'm writing code in the gedit text editor and then running the code in the terminal using the python command. this could probably apply to any language but I'm learning python at the moment – Sian Pike Jan 26 '15 at 21:57
  • We would love to help, but we need to know what you are trying to do. Please read you question, imagine you are not you, imagine you are me, I do not know what you know, I do not know what you are trying to do. Now tell be what you are trying to do. Where are you when you want to show one line. This one line, what is it one line of (the python program, output from python program, input, something else). Please improve question, don't add comment. – ctrl-alt-delor Jan 26 '15 at 22:02
  • It's literally a really simply program, it's just a load of lines allowing me to print something like print("a line") and I want to just see line 5 for example when running the program in the terminal, I'm pretty new to programming so I'm not sure how to explain it much clearer, I'm sorry – Sian Pike Jan 26 '15 at 22:04
  • Just guessing here... to just print 9'th line from a file, say sed -n '9{p;q}' yourfile.py. But unless this has some profound scripting use, you could just as easily find the wanted line in a text editor. If you want to evaluate just one line in python, this is not the best idea. Also, you can experiment in interactive python shell much better -- print out arbitrary variables, check function definitions, use command history. – orion Jan 26 '15 at 22:09
2

Assuming you want to view the nth line of a file, you could simply do:

sed -n '42p' yourfile.py

Replace 42 with whatever line number you want to see.

After your edit: If you also want to execute this code, simply pipe it to python:

sed -n '42p' yourfile.py | python
1

Alternate awk solution:

awk "NR==2" programname.py
0

If you're not comfortable with bash/shell, you can write a python script to do that too!

#!usr/bin/env python
# usage: ./script file line_no
import sys
data = ''
with open(sys.argv[1],'r') as f:
    data = f.read()
data = data.split('\n')
try:
    print data[sys[2] +1]
except IndexError:
    print "Line doesn't exist"

NOTE: This is just a quick and dirty example. You can improve it to your liking.

  • Ahh. Didn't notice this is an old question. My bad :') – turtleDev Jun 3 '15 at 18:46
-2

Based on @DoorKnob answer, and in case of someone finding this in a search and wanting to find nth line of output. Then do this.

run-program | sed -n '2p'

This will print the 42nd output line of run-program

e.g.

python yourfile.py | sed -n `2p`
  • The question clearly states "I only want the code in line two to appear in the terminal". – pzkpfw Jan 27 '15 at 13:16
  • Sorry I did not read it properly, I have updated the answer in accordance to your comment. – ctrl-alt-delor Jan 28 '15 at 21:23
  • They're both incorrect because they're both filtering program output. You're probably better of deleting the answer since there is another one already accepted. – pzkpfw Feb 10 '15 at 7:50

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.