Linux file operation tips: delete multiple lines at the end

PHPz
Release: 2024-03-01 18:09:03
Original
769 people have browsed it

Linux file operation tips: delete multiple lines at the end

When using the Linux operating system for file processing, you often encounter situations where you need to delete multiple lines at the end of the file. This operation can usually be achieved through some simple commands. The following will introduce some common Linux file operation techniques and provide specific code examples.

  1. Use the sed command to delete multiple lines at the end:

The sed command is a stream editor that can be used to process text. By combining the sed command and regular expressions, you can easily delete multiple lines at the end of the file.

The specific code is as follows:

sed -i '$d' file.txt
Copy after login

In this command,-imeans directly modifying the file content,$means matching the last line of the file,dmeans to delete the matching lines. After executing this command, the line at the end of filefile.txtwill be deleted.

If you need to delete multiple lines at the end, you can use the following command:

sed -i '{start_line_number},{end_line_number}d' file.txt
Copy after login

where{start_line_number}is the starting line number that needs to be deleted from the end.{end_line_number}is the end line number to be deleted. After executing this command, the contents from the starting line to the ending line in the filefile.txtwill be deleted.

  1. Use the head command to delete multiple lines at the end:

The head command is usually used to display the header content of the file, but it can also be combined with-nParameter to choose to display the first few lines of the file.

The specific code is as follows:

head -n -$N file.txt > temp.txt && mv temp.txt file.txt
Copy after login

where$Nrepresents the number of rows that need to be deleted. In this command,head -n -$N file.txtwill output the contents of the file except the last N lines to thetemp.txttemporary file, and then pass ## The #mvcommand renames the temporary file to the original file namefile.txt, thereby achieving the effect of deleting multiple lines at the end.

Through the above introduction, we can use the sed command or the head command to delete multiple lines at the end of the file to achieve rapid file processing. At the same time, we can also make adjustments according to specific needs and flexibly use these techniques to improve the efficiency of file operations.

The above is the detailed content of Linux file operation tips: delete multiple lines at the end. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!