The Linux Newbie Guide  ⇒    Fundamentals     Advanced     Supplement   Command Index   ENG⇒中
All rights reserved, please indicate the source when citing
  

sed - Text Data Processing





The primary function of sed is to automate the modification of text files and it provides excellent support for regular expressions. The most basic usage of sed involves the "s" command, which is used to substitute one string with another. The syntax is sed s/OLD/NEW/, where "NEW" replaces "OLD."

Example:
$ echo 'good morning google' | sed s/o/O/ ←Changes the lowercase "o" to uppercase "O"
gOod morning google
$ echo 'good morning google' | sed s/o/OOOO/ ←Changes the "o" to "OOOO"
gOOOOod morning google


By default, sed replaces only the first occurrence of a pattern. To replace all occurrences, you need to add the "g" flag, like this: sed s/OLD/NEW/g

Example:
$ echo 'good morning google' | sed s/oo/OO/g ←Changes all instances of "oo" to "OO"
gOOd morning gOOgle


sed has its own scripting language, making its usage relatively complex and not easily explained in just a few lines. However, it's worth learning, and if you're interested in delving deeper, you can refer to the provided link for more information.