this command displays the second line of the file :
cat myfile | head -2 | tail -1
My file contains the following data :
hello mark this is the head line this is the first line this is the second line this is the last line
the command above prints the data as: mark
But i am unable to understand this because, head -2 is used to print the first two lines and tail -1 prints the last line but how come 2nd line is printed!!???
tail displays the last line of the head output and the last line of the head output is the second line of the file. Show activity on this post. If you break up operations into separate commands, it will become obvious why it works the way it works. head -2 creates a file of two lines.
To look at the last few lines of a file, use the tail command. tail works the same way as head: type tail and the filename to see the last 10 lines of that file, or type tail -number filename to see the last number lines of the file.
Use the tail command to write the file specified by the File parameter to standard output beginning at a specified point. This displays the last 10 lines of the accounts file. The tail command continues to display lines as they are added to the accounts file.
You can also use "sed" or "awk" to print a specific line:
EXAMPLE:
sed -n '2p' myfile
PS: As to "what's wrong with my 'head|tail'" command - shelltel is correct.
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