SED basic usage:
Sed:stream Editor Editor
Each time the line editor reads a line of text into memory space, this space is called pattern space. By default, the original file is not edited, only the data in the pattern space is processed, and when finished, the contents of the pattern space are displayed.
sed [OPTION] ... {Script-only-if-no-other-script} [Input-file] ...
-n Silent mode, does not display the contents of the mode space by default
-I directly modify the original file
-e script can execute multiple scripts simultaneously
-f/path/to/sed_script read SED script from file
-R using extended regular expressions
Sed ' addresscommmand ' file ...
ADDRESS:
1.addr1,addr2
2.$
3./regexp/
4./pattern1/,/pattern2/
5.LINENUMBER
6.addr,+n
COMMAND:
D:delete reads the text into the pattern space, deletes the qualifying rows, and displays the remaining content in the pattern space
Sed ' 2,5d '/etc/fstab
P:print reads the text into the pattern space, prints the rows that match the criteria, displays the pattern space and the printed lines, and if you want to display only the printed rows, use the-n option
Sed-n ' 1,4p '/etc/fstab
A \string: Appends a new line of "STRING" after the specified line
I \string: Insert a new line of "STRING" before the specified line
R file: Adds the contents of the specified file to the line following the qualifying row
W File: Writes a line in the specified range of addresses to the specified file
s/pattern/string/modifier: Find substitution, default replaces only the first match to the string in each line.
G: Global Substitution
I: Ignore character case
s///s### [Email protected]@@
\ (\) \1 \2
&: Indicates reference to the previously matched string
Practice:
1. Delete the white space character at the beginning of the history command
History|sed-r ' s/^\s+//'
2. Remove the parent directory of the given path
"/tmp/log/messages/hello" |sed-rn ' s#^/.*/([^/]+)/?#\1#GP '
#linux基础 #sed First Contact