Bash
Scripting
Command Line
File Manipulation
Programming

Print a file, skipping the first X lines, in Bash

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

When working with text files in a Bash environment, you might often need to manipulate these files before processing them further. One common task is to print the contents of a file but skip the first x lines. This type of operation is useful in scenarios where the initial lines are headers, metadata, or otherwise not required for the particular data processing application you are performing.

Using tail

The tail command in Bash can be used effectively to skip a certain number of lines at the start of a file and print the rest. The syntax for tail is quite straightforward. Here's how you can use it:

bash
tail -n +X file.txt

Here, X represents the number of lines to skip plus one. For instance, if you want to skip the first 5 lines, you would use:

bash
tail -n +6 file.txt

This tells tail to start reading from the 6th line, effectively skipping the first 5 lines.

Using sed

Another powerful tool for text processing within Unix-like systems is sed, the stream editor. To skip the first X lines of a file and output the rest, you can use:

bash
sed '1,Xd' file.txt

In this case, replace X with the number of lines you want to skip. For example, to skip the first 5 lines:

bash
sed '1,5d' file.txt

The command 1,5d tells sed to delete lines from 1 to 5.

Using awk

awk is another potent tool for text processing which can be used to skip a certain number of lines in a file:

bash
awk 'NR > X' file.txt

Here, NR stands for "Number of Record". If you set X to 5, awk will start processing from the 6th line:

bash
awk 'NR > 5' file.txt

Practical Example

Consider a file named example.txt with the following content:

 
1Header1
2Header2
3Data1
4Data2
5Data3

To skip the two header lines and print only the data lines, you can use any of the commands described:

  • Using tail:
bash
  tail -n +3 example.txt
  • Using sed:
bash
  sed '1,2d' example.txt
  • Using awk:
bash
  awk 'NR > 2' example.txt

Summary Table

CommandSyntax ExampleDescription
tailtail -n +6 file.txtSkips the first 5 lines and prints the rest from the 6th line.
sedsed '1,5d' file.txtDeletes the first 5 lines, effectively only printing from the 6th line onwards.
awkawk 'NR > 5' file.txtProcesses the file starting from the 6th line, skipping the first 5 lines.

Additional Tips

  • When using these commands in scripts, it's important to handle errors such as files not existing or being unreadable.
  • Combining these commands with others like grep, sort, or cut can help in generating powerful one-liner commands for text processing in Bash scripts.

These methods provide a robust set of options for manipulating file content right from the command line, giving users the flexibility to manage files effectively and efficiently in various data processing tasks.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions