Linux sed stream editor

Sed is the abbreviation of stream editor, which is the stream editor. Stealing a picture to explain the principle

 

Command format:

SYNPPSIS: sed [OPTION]… {script-only-if-no-other-script} [input-file]…

Options:

-n use silent mode (can't figure out why it's not -s) -e edit
 sed actions directly on the command line mode; -f write sed actions directly in a file, -f filename can execute filename The sed command inside;
 -r makes the sed command support extended regular expressions (the default is the basic regular expression);
 -i directly modifies the content of the file read, rather than output by the screen.

Common commands

a \text append means appending a string, a \ followed by the string s (multi-line strings can be separated by \n), the string s will be added after the currently selected line;
c \text replace/replace the string, c \ followed by the string s (multi-line strings can be separated by \n), the currently selected line will be replaced with the string s;
d means delete, this command will delete the currently selected line;
i \text insert means inserting a string, i \ followed by the string s (multi-line strings can be separated by \n), the string s will be inserted in front of the currently selected line;
p print is to print, this command will print the currently selected line to the screen;
s/pattern/string/ replacement, usually the usage of the s command is like this: 1, 2s/old/new/g, replacing the old string with the new string separator can make / # @ and other modifiers g: global i : ignore case 
w /PATH/TO/SOMEFILE keep the lines matched by the pattern space to the specified file

example:

Save the line containing root in passwd in the abc file in the current directory

# sed '/root/w ./abc' passwd

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325340713&siteId=291194637