How to Delete Specific Lines from a File Using Line Numbers
Learn multiple ways to remove specific lines from a file using tools like sed, awk, Python, and more—with easy-to-follow examples.

When you're working with text files—be it config files, logs, or source code—you may need to delete specific lines based on their line numbers. This might sound intimidating, but it’s actually quite easy once you know which tool to use.
In this post, we’ll walk through several methods to remove lines using line numbers, using command-line tools like sed, awk, and even Python. Whether you're a beginner or a seasoned developer, there’s a solution here for you.
The Basic Idea
To delete a specific range of lines from a file:
- Identify the start line and end line.
- Use a tool or script to remove the lines between those numbers.
- Save the changes back to the original file.
Let’s break this down by method.
1. Using sed (Stream Editor)
sed is a command-line utility that’s perfect for modifying files line-by-line.
Basic Syntax
sed 'START_LINE,END_LINEd' filename > temp_file && mv temp_file filename
- Replace
START_LINEandEND_LINEwith actual numbers. dtellssedto delete those lines.
Example
To delete lines 10 through 20:
sed '10,20d' myfile.txt > temp_file && mv temp_file myfile.txt
With Variables
START_LINE=10
END_LINE=20
sed "${START_LINE},${END_LINE}d" myfile.txt > temp_file && mv temp_file myfile.txt
2. Using awk
awk is a pattern scanning tool. It’s ideal for skipping specific lines.
Syntax
awk 'NR < START_LINE || NR > END_LINE' filename > temp_file && mv temp_file filename
Example
awk 'NR < 10 || NR > 20' myfile.txt > temp_file && mv temp_file myfile.txt
This prints all lines except lines 10 through 20.
3. Using head and tail
Perfect when you only need to chop lines off the start or end.
Example
Delete lines 10 to 20:
head -n 9 myfile.txt > temp_file
tail -n +21 myfile.txt >> temp_file
mv temp_file myfile.txt
head -n 9gets lines before line 10.tail -n +21grabs everything from line 21 onward.
4. Using perl
perl is great for more advanced file manipulation.
Syntax
perl -ne 'print unless $. >= START_LINE && $. <= END_LINE' filename > temp_file && mv temp_file filename
Example
perl -ne 'print unless $. >= 10 && $. <= 20' myfile.txt > temp_file && mv temp_file myfile.txt
$.is the line number variable inperl.
5. Using Python
For full control or if you’re already using Python in your workflow:
Example
start_line = 10
end_line = 20
with open("myfile.txt", "r") as file:
lines = file.readlines()
with open("myfile.txt", "w") as file:
for i, line in enumerate(lines):
if i < start_line - 1 or i > end_line - 1:
file.write(line)
Python is especially useful if you need to add logic or conditions around what gets deleted.
📚 Working with files in Python
Conclusion
There are plenty of ways to delete lines from a file based on line numbers:
- Use
sedfor simple, fast command-line editing. - Choose
awkfor conditional line selection. - Go with
head/tailfor edge-case trimming. - Try
perlif you’re comfortable with regex and quick one-liners. - Opt for Python when you need logic-heavy, readable scripts.
Explore Nife.io for modern cloud infrastructure solutions, or check out OIKOS to see how edge orchestration is done right.
Deploy on Nife
Ready to deploy? Check out the Nife documentation:
From the team at Nife
Deploy your next app in minutes, not days.
Nife is a multi-cloud edge platform that lets you deploy, scale, and manage applications globally without the infrastructure headache.