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.
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.
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.
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.
head -2 myownfile | tail -1
should do what you want
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With