Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print a file, skipping the first X lines, in Bash [duplicate]

I have a very long file which I want to print, skipping the first 1,000,000 lines, for example.

I looked into the cat man page, but I did not see any option to do this. I am looking for a command to do this or a simple Bash program.

like image 381
Eduardo Avatar asked Mar 03 '09 02:03

Eduardo


People also ask

How do you skip the first line of a file in shell?

The following `awk` command uses the '-F' option and NR and NF to print the book names after skipping the first book. The '-F' option is used to separate the content of the file base on \t. NR is used to skip the first line, and NF is used to print the first column only.

How do I skip a line in bash?

Using head to get the first lines of a stream, and tail to get the last lines in a stream is intuitive. But if you need to skip the first few lines of a stream, then you use tail “-n +k” syntax. And to skip the last lines of a stream head “-n -k” syntax.

How do I duplicate a file in bash?

Copy a File ( cp ) You can also copy a specific file to a new directory using the command cp followed by the name of the file you want to copy and the name of the directory to where you want to copy the file (e.g. cp filename directory-name ).

How do I get the first 10 lines of a file in Linux?

To look at the first few lines of a file, type head filename, where filename is the name of the file you want to look at, and then press <Enter>. By default, head shows you the first 10 lines of a file. You can change this by typing head -number filename, where number is the number of lines you want to see.


1 Answers

You'll need tail. Some examples:

$ tail great-big-file.log < Last 10 lines of great-big-file.log > 

If you really need to SKIP a particular number of "first" lines, use

$ tail -n +<N+1> <filename> < filename, excluding first N lines. > 

That is, if you want to skip N lines, you start printing line N+1. Example:

$ tail -n +11 /tmp/myfile < /tmp/myfile, starting at line 11, or skipping the first 10 lines. > 

If you want to just see the last so many lines, omit the "+":

$ tail -n <N> <filename> < last N lines of file. > 
like image 81
SingleNegationElimination Avatar answered Sep 24 '22 05:09

SingleNegationElimination