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.
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:
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:
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:
In this case, replace X with the number of lines you want to skip. For example, to skip the first 5 lines:
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:
Here, NR stands for "Number of Record". If you set X to 5, awk will start processing from the 6th line:
Practical Example
Consider a file named example.txt with the following content:
To skip the two header lines and print only the data lines, you can use any of the commands described:
- Using
tail:
- Using
sed:
- Using
awk:
Summary Table
| Command | Syntax Example | Description |
tail | tail -n +6 file.txt | Skips the first 5 lines and prints the rest from the 6th line. |
sed | sed '1,5d' file.txt | Deletes the first 5 lines, effectively only printing from the 6th line onwards. |
awk | awk 'NR > 5' file.txt | Processes 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, orcutcan 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.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.