Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using combination of "head" and "tail" to display middle line of the file in Unix

If I have a file name myownfile.txt which contains 3 lines of text.

foo
hello world
bar

I want to display the line in the middle which is hello world by using head and tail command only.

like image 963
Ali Avatar asked Nov 01 '11 19:11

Ali


People also ask

How do you show the middle line in Unix?

Using combination of "head" and "tail" to display middle line of the file in Unix - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.

How do you use head and tail together?

To read the entire file, 'cat', 'more', and 'less' commands are used. But when the specific part of the file is required to read then 'head' and 'tail' commands are used to do that task. 'head' command is used to read the file from the beginning and the 'tail' command is used to read the file from the ending.

Can head and tail be used together in Linux?

Use the head and the tail Together We've learned that the head command can give us the first part of a file, while the tail command can output the last part of the input file.


2 Answers

head -2 myownfile | tail -1 

should do what you want

like image 148
ennuikiller Avatar answered Sep 21 '22 05:09

ennuikiller


head -2 displays first 2 lines of a file

$ head -2 myownfile.txt
foo
hello world

tail -1 displays last line of a file:

$ head -2 myownfile.txt | tail -1
hello world
like image 26
user3287432 Avatar answered Sep 23 '22 05:09

user3287432