Sed is an online editor that handles a single line of content at a time. When processing, the currently processed rows are stored in a temporary buffer, known as pattern spaces, followed by the SED command to handle the contents of the buffer, and the contents of the buffer are sent to the screen after processing is complete. The next line is processed so that it repeats until the end of the file. The contents of the file do not change unless you use redirected storage output.
Basic commands for SED:
1. Replace: s command
1.1 Basic usage
such as: sed ' s/day/night/' <old >new
This example replaces the first occurrence of day in the file old with night, outputting the result to the file new
S "Replace" command
/.. /.. /split character (Delimiter)
Day search string
Night Replacement string
In fact, the separator "/" can be replaced with other symbols, such as ",", "|" and so on.
such as: sed ' s/\/usr\/local\/bin/\/common\/bin/' <old >new
Equivalent to sed ' s_/usr/local/bin_/common/bin_ ' <old >new
Obviously, it is much better to use "_" as a separator than "/".
1.2 Matching string with &
Sometimes you might want to add some characters around or around a string that matches.
such as: sed ' s/abc/(ABC)/' <old >new
This example adds parentheses around the found ABC.
This example can also be written as Sed ' s/abc/(&)/' <old >new
The following are more complex examples:
Sed ' s/[a-z]*/(&)/' <old >new
SED replaces only the first occurrence of the search string by default, using/g to replace the search string for all
$ sed ' s/test/mytest/g ' example-----Replace test with mytest across the entire line. If there is no G-tag, only the first matching test for each row is replaced with mytest.
$ sed ' s/^192.168.0.1/&localhost/' example-----& symbol represents the part found in the replacement string. All rows beginning with 192.168.0.1 are replaced with localhost, which becomes 192.168.0.1localhost.
$ sed ' s#10#100#g ' example-----no matter what character, followed by the S command is considered a new delimiter, so, "#" Here is the separator, instead of the default "/" separator. means to replace all 10 with 100.
If you need to make multiple modifications to the same file or row, you can use the "-e" option
Obtain eth0 network card IP address: