Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unix- show the second line of the file

Tags:

unix

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!!???

like image 333
Chandeep Avatar asked Dec 12 '12 04:12

Chandeep


People also ask

How do I read a second line in Unix?

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.

How do I see the last second line of a file in Linux?

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.

How do we display the last second line of a 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.


1 Answers

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.

like image 123
paulsm4 Avatar answered Nov 10 '22 01:11

paulsm4