Basic usage of the Linux BOC editor sed
How to deal with SED
Reads the file into memory by default----> reads conforming rows into memory that belongs to SED (the mode space of sed) for editing----> display the contents of the post-processing mode space on the screen
Sed: line editor, which does not edit the source file by default, only handles the display of data in the pattern space
How to use
sed [OPTION] ... {Script-only-if-no-other-script} [Input-file].
sed [OPTION]: ' Linesrangecommand ' File ...
[OPTION]:
-N: Silent mode, no longer displays the contents of the mode space by default
-I: Modify source files directly
-E ' linesrangecommand '-e ' linesrangecommand ': Perform multiple operations simultaneously
-F: Specifies a script file that holds multiple SED commands to perform multiple operations simultaneously
-R: Means using an extended regular expression, using only regular expressions by default
Linesrange: Represents a range, that is, which rows
Usage 1:startline,endline such as 1,100 from line 1th to line 100th
Usage 2:/pattern/For example/^redhat/all rows starting with a line beginning with Redhat
Usage 3:/pattern1/,/pattern2/the first line to be matched by PATTERN1,
To the end of the line that was first matched to the pattern
Line: The specified row
Startline,+n N rows Backward from StartLine
Command: Indicates the commands
D: Delete rows that match the criteria
Sed ' 2, $d '/etc/passwd
P: Show rows that match the criteria
Sed '/home/p '/etc/passwd
A \string: Appends a new line after the specified line, with the contents of string
Sed '/^root/a \i am root '/etc/passwd
I \string: Adds a new row before the specified line, with the contents of string
Sed '/^root/i \i am root '/etc/passwd
R file: After you add the entire contents of the FILE1 file to the FILE2 line that matches the criteria,
For merging files
Sed ' 2r FILE1 ' FILE2
W File: Save eligible rows in the FILE2 file to the FILE1 file
Sed ' 2w FILE1 ' FILE2
s/pattern/string/: Find and Replace,
The default is to replace only the first matched string in each row
G: Global Replacement s/pattern/string/g
I: Ignore character case s/pattern/string/i
Sed ' 1,2s/oot/oot/'/etc/fstab
Back reference \ (string\), \1,\2
Sed ' s#\ (L.. e\) #\1r#g ' Test.txt
&: pattern matching such as sed ' s/l. E/&r/g ' Test.txt
After the string that matches the preceding pattern, add
This article is from the "surgery industry has specialized" blog, please be sure to keep this source http://fuvip.blog.51cto.com/9276123/1980853
Basic usage of the Linux BOC editor sed