The SED command is similar to awk except that it is simpler than awk. Awk is more complex and more powerful. SED stands for Stream EDitor. It is mainly processed by behavior units, which can replace, delete, add, select and other specific work of data lines. In particular, sed does not modify the source file.

Method of use

Basic usage

sed [-nei] 'command' filename
Copy the code

Parameters that

  • -nUse quiet mode. Only the processed line is displayed.(Normally, everything is displayed on the screen.)
  • -eSed action editing is performed directly in command column mode
  • -iModify the contents of the read file directly, rather than having screen output

Core commands

  • a new,a can be followed by strings that appear on a new line (currently the next line)
  • c replaceC can be followed by strings that replace lines between n1 and n2
  • d deleteD is not followed by any string.
  • i insert, I can be followed by strings, which will appear on a new line (the previous line)
  • p print“Is usually used with sed-n
  • s replaceUsually this s action can be used with a regular expression.
  • nRead the next line
  • yThe specifiedreplace
# a for A,b for B,c for C
sed 'y/abc/ABC' file
Copy the code
  • q exit

The sample demo

  • Display lines 40 to last

    sed -n '40,$p' /etc/passwd
    Copy the code

  • Add string “===I am here===” after line 40 to last line

    sed '40,$a ===I am here===' /etc/passwd
    Copy the code

  • Query sys characters by mode

    sed -n '/sys/p' /etc/passwd
    Copy the code

  • Replace part of a line

    sed -n 's/sys/replace/p' /etc/passwd
    Copy the code

reference

  1. Wikipedia sed
  2. Sed website

If this article is helpful to you, or you are interested in technical articles, you can follow the wechat official number: Technical Tea Party, and you can receive relevant technical articles in the first time. Thank you!

This article was automatically published by ArtiPub