Saturday, May 10, 2014

Linux - Printing first or last 10 lines

There are many examples when we need to print first or last lines of some file. Commands head and tail ca help to do this.

Command head prints 10 first lines of file.

$ head file

or

$ cat file|head

There is command line argument -n, which says how many lines to be printed.

Example:

$ head -n 5 file

prints first 5 linest from file.

if number is negative, command head prints last n lines from file.

Command tail does oposite from command head: prints last 10 lines from a file.

$ tail file

same as command head there is argument -n which tells to command head how many last lines from a file is to be printed:

$ tail -n 7 file

prints last 7 lines from file.

If you want to print all lines in file excluded first n lines, command is:

$ tail -n +5 file

prints all lines in file, excluded first 5 lines.

Command tail is very useful in log analyzing, when you are not interested in whole file, but only last n lines.

No comments:

Post a Comment