LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Print line number X of a file (in shell) (https://www.linuxquestions.org/questions/programming-9/print-line-number-x-of-a-file-in-shell-273849/)

rheza 01-04-2005 03:50 PM

Print line number X of a file (in shell)
 
I didn't think I need it a while ago so I excuse me for another topic.

Is there a way to print line number X of a certain file.

For example:

file "file" has 3 lines:

hello
abc
hi

Is there a simple command to print say, the second line of file "file":

abc

itsme86 01-04-2005 03:55 PM

If nothing else you could just do:
Code:

head -n $num $file | tail -n 1

jim mcnamara 01-04-2005 03:59 PM

Assume you want to print just line 29 ---
this is one way to do it:
Code:

head -29 myfile | tail -1

rheza 01-04-2005 04:03 PM

Thanks. It's an odd way, but I suppose this is how it's usually/(supposed to be) done. XD

Hko 01-04-2005 05:55 PM

There's also sed:
Code:

sed -n 17p yourfile.txt


All times are GMT -5. The time now is 02:28 AM.