How to Print File After Skipping First X Lines in Bash

Abdullah Bukhari Feb 02, 2024
  1. Skip Lines Using the head and tail Commands
  2. Use vim to Skip First n Lines
  3. Use sed to Skip First n Lines
How to Print File After Skipping First X Lines in Bash

Suppose you have a file, a huge one, and you want to display its contents. How would you go about it?

You obviously wouldn’t want to print out the file’s entire contents as that wouldn’t be very practical. You would want to print some selective lines; maybe you’d use regular expressions to parse the file and only print the matches.

This article will explain several methods to do so.

Skip Lines Using the head and tail Commands

The below example uses the five most commonly used Bash commands to do so:

tail fileName.txt
head fileName.txt
tail -n fileName.txt # here n is an integer
head -n fileName.txt # here n is an integer
tail +n fileName.txt # prints all lines starting from
# the nth file

What the tail command does is that it prints the last ten lines of a file, while the head prints the first ten lines of a file.

When you use the tail command with the –n option, it prints the last n lines. Conversely, when you use the head command with the –n option, it prints the first n lines.

It is important to note that here n is an integer, so when you execute the commands, you will have to substitute n with an integer.

The fifth method is somewhat tricky. It ignores the first (n – 1) lines and prints all the lines after that.

Use vim to Skip First n Lines

You could also use the Vim editor to skip the first n lines. Vim is a console-based text editor that allows you to create and change any text document highly efficiently.

To first use Vim, you must install it. Use the command below to do so:

sudo apt install vim

Now that Vim is installed, we get to the business part of our purpose, which is using Vim to skip the first n lines.

We will do it by using an intermediary file. We will first copy the contents of our old file to a new one; then, we will delete the new file’s first n lines.

We will use input and output redirection to copy the contents of one file to another. If you have studied the operating system course, you would have heard of the ppfdt table (process file descriptor table).

By default, the first descriptor points to stdin (or keyboard), the second descriptor points to stdout (or monitor), and the third descriptor points to stderror.

Consider the script below for further understanding.

cat 0<old_file.txt 1>new_file.txt # copies old file’s contents to new file

The above command reads the contents of the old file and copies it to the new file. Notice how we use the descriptor 0 to read and the descriptor 1 to write.

If you find this descriptor confusing, we’ve got something different for you. The below commands will work too:

cat <old_file.txt >new_file.txt # copies old file’s contents to new file
cp <old_file.txt >new_file.txt # copies old file’s contents to new file

Please notice that in the second method above, we’ve used the copy command (i.e., cp). It takes two arguments: the path to the source file and the path to the destination file.

Now that we’re done copying the file. Open the new file in Vim and use the below command:

vim new_file.txt

Now use Shift+Esc and type the following command in Vim:

:1,nd # here n is an integer number e.g., 2

The above command deletes the first n lines in the new_file.txt. Here, d stands for delete.

Use sed to Skip First n Lines

Creating a new file and then deleting its contents can be a hassle. Furthermore, it will also consume a lot of extra memory if the old file is huge.

So, let’s see an easier method to achieve the same:

sed 1,nd old_file.txt # here n is an integer

It is important to note that the sed command will not modify the old file. Rather, it will simply display the contents of the old file after removing the first n lines.