In this article, we will discuss multiple ways to delete the last N lines from a file in Linux. For this purpose, we will use various Linux utilities like sed, awk, and head.

Advertisement

Before we proceed, it’s important to mention that manipulating files directly on a Linux system can potentially be destructive if not done properly. Always make sure you have a backup of your file before performing these operations, especially if the file contains important data.

1. Using the head command

The head command in Linux is used to print the top N number of data of the given input. By default, it prints the first 10 lines of the specified files. If more than one file name is provided, then it prints the top 10 lines from each file, precedes with its name.

You can use the head command to delete the last N lines from a file by printing all lines except the last N lines like this:

Where ‘N’ is the number of lines you want to delete from the end. Here is an example:

head -n -10 myfile.txt 

This command will print all lines of ‘myfile.txt’ except the last 10. However, to write the changes to the file, you need to redirect the output back into the file:

head -n -10 myfile.txt > temp.txt && mv temp.txt myfile.txt 

This command writes the output to a temporary file and then replaces the original file with the temporary file.

2. Using the sed command

sed, short for Stream Editor, is a powerful command-line utility used for parsing and transforming text. It’s especially handy when dealing with text files.

To delete the last ‘N’ lines of a file using sed, you can use the following command:

Where ‘N’ is the number of lines from the end. Here is an example:

sed -i '$10,$d' myfile.txt 

This command will delete the last 10 lines from ‘myfile.txt’. The ‘-i’ option tells sed to edit files in place (i.e., saving changes to the original file).

3. Using the awk command

awk is another powerful command-line tool in Unix/Linux. It’s an excellent filter and report writer. Many times, it’s used when sed or grep are not enough to process text, mainly when dealing with columns or fields of data.

Here is how you can use awk to delete the last N lines from a file:

Where ‘N’ is the number of lines you want to delete from the end. Here is an example:

awk -v n=10 'NR>n{print a[NR%n]} {a[NR%n]=$0}' myfile.txt 

This command will print all lines of ‘myfile.txt’ except the last 10. Similar to the head command, to write the changes back into the file, you need to use redirection:

awk -v n=10 'NR>n{print a[NR%n]} {a[NR%n]=$0}' myfile.txt > temp.txt && mv temp.txt myfile.txt 

This command writes the output to a temporary file and then replaces the original file with the temporary file.

Conclusion

As you can see, there are multiple ways to delete the last N lines from a file in Linux. Depending on your specific needs and the complexity of your tasks, you may choose the one that suits you best. Remember that while these operations can be very useful, they can also be destructive if not done correctly, so always make sure you have a backup of your data before performing these operations.

If you find it difficult to remember these commands, you can write a script that performs these operations and call the script when you need to delete the last N lines from a file. This way, you only need to remember how to call the script, not the specific command to use.

Share.
Leave A Reply


Exit mobile version