Home>Article>Operation and Maintenance> How to delete a line of content in linux

How to delete a line of content in linux

WBOY
WBOY Original
2021-12-31 15:58:01 14810browse

In Linux, you can use the sed command to delete a specified line of content. This command can replace, delete, add, select, etc. data lines. When the parameter is set to "d", you can delete the line. The content, the syntax is "sed 'the specified row value d' that needs to be deleted".

How to delete a line of content in linux

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

How to delete a line of content in Linux

sed is a stream editor. It is a very good tool for text processing and can perfectly Used with regular expressions, the function is extraordinary.

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 content in the buffer. After the processing is completed, the buffer content is 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, and writing conversion programs.

The Linux sed command uses scripts to process text files.

sed can process and edit text files according to the instructions of the script.

Sed is mainly used to automatically edit one or more files, simplify repeated operations on files, write conversion programs, etc.

The syntax is:

sed [-hnV][-e
       
  • -f
  • -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: Newly 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 any dong dong;

    • i: Insert, after i Strings can be connected, 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 this s action can be paired with regular notation! For example, 1,20s/old/new/g is it!

    The example is as follows:

    List the contents of /etc/passwd and print the line number. At the same time, please delete lines 2~5!

    How to delete a line of content in linux

    Just delete the 2nd line

    nl /etc/passwd | sed '2d'

    To delete the 3rd to the last line

    nl /etc/passwd | sed '3,$d'

    Related recommendations: "

    Linux Video Tutorial

    The above is the detailed content of How to delete a line of content 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