Linux uses the sed command to perform line operations on files.

title: "Using sed command in Linux for file line operations"
date: "2018-11-09"

  • "system management"
  • "linux"
  • "sed"

  1. Delete the first line of the document: sed -i '1d'
  2. Delete the last line of the document: sed -i '$d'
  3. Add a line after a specified line in the document. For example, if the document is as follows: echo "1"; echo "2"; echo "4"; echo "5"; and you want to add echo "3" after echo "2", you can use the following command: sed -i '/echo "2";/aecho "3";' The use of semicolon is because it already exists in the text. In other words, the semicolon is not necessary! In general, the command is: sed -i '/* /a*'
  4. Delete a specific line in the file: sed -i '3d'
  5. Delete all lines in the file that start with a specific keyword: sed -i '/^QWQ/d'
  6. Delete all lines in the file that contain a specific keyword: sed -i '/QWQ/d'
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.