Home >Operation and Maintenance >Linux Operation and Maintenance >How to delete the first few lines of a file in Linux

How to delete the first few lines of a file in Linux

WBOY
WBOYOriginal
2022-03-21 10:50:5312066browse

In Linux, you can use the sed command to delete the first few lines of a file. This command can process and edit text files according to the instructions of the script. It can be used with regular expressions to achieve the effect of deleting the first few lines of the file. Syntax It is "sed -i '1, number of first few lines d' file name".

How to delete the first few lines of a file in Linux

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

How to delete the first few lines of a file in Linux

sed -i '1,nd' 文件名
这个是删除第一行到第n行

sed is a stream editor. It is a very good tool for text processing. It can be used perfectly with regular expressions and has extraordinary functions. . During processing, the currently processed line is stored in a temporary buffer, called "pattern space", and then the sed command is used to process the contents of the buffer. After the processing is completed, the contents of the buffer are sent to the screen. Then process the next line, and repeat until the end of the file. The file contents are not changed unless you use redirection to store the output.

Sed is mainly used to automatically edit one or more files. It can perform specific tasks such as replacing, deleting, adding, and selecting data lines, simplifying repeated operations on files, writing conversion programs, etc.

Syntax

sed [-hnV][-e<script>][-f<script文件>][文本文件]

Parameter description:

  • -e3f1c4e4b6b16bbbd69b2ee476dc4f83a or --expression=3f1c4e4b6b16bbbd69b2ee476dc4f83a with the script specified in the option Process input text files.

  • -f8d9e20cf7980a42776913d042440355f or --file=8d9e20cf7980a42776913d042440355f processes the input text file with the script file specified in the option.

  • -h or --help displays help.

  • -n or --quiet or --silent only displays the results after script processing.

  • -V or --version displays version information.

Action description:

  • a: Added, a can be followed by strings, and these strings will appear on a new line (The current next line)~

  • c: Replacement, c can be followed by strings, and these strings can replace the lines between n1 and n2!

  • d: Delete, because it is deletion, so d is usually not followed by anything;

  • i: Insert, after i You can receive strings, and these strings will appear on a new line (the current previous line);

  • p: Print, that is, print out a selected data. Usually p will be run together with the parameter sed -n~

  • #s: Replacement, you can directly perform the replacement work! Usually the action of this s can be paired with a regular representation! For example, 1,20s/old/new/g is it!

The example is as follows;

Delete the first line to the 14030000th line in front of send_url_log.txt

sed -i ‘1,14030000d’ send_url_log.txt

Related recommendations:《Linux Video tutorial

The above is the detailed content of How to delete the first few lines of a file in Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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