How to Delete Specific Lines from a File Using Line Numbers

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:

  1. Identify the start line and end line.
  2. Use a tool or script to remove the lines between those numbers.
  3. 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_LINE and END_LINE with actual numbers.
  • d tells sed to 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

📚 More on sed line deletion


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.

📚 Learn more about awk


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 9 gets lines before line 10.
  • tail -n +21 grabs everything from line 21 onward.

📚 tail command explained


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 in perl.

📚 Perl I/O Line Numbering


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 sed for simple, fast command-line editing.
  • Choose awk for conditional line selection.
  • Go with head/tail for edge-case trimming.
  • Try perl if 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.