First, sed Use of Commands
Sed is an online editor that processes a single line of content at a time. When processing, the currently processed rows are stored in a temporary buffer called pattern space, followed by the SED command to process the contents of the buffer, and after processing is done, the contents of the buffer are sent to the screen. Then the next line is processed, so it repeats until the end of the file. The file content does not change unless you use redirection to store the 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 day of the first occurrence of each line in the file old with night, outputting the result to the file new
S "Replace" command
/.. /.. /Separator (Delimiter)
Day search string
Night Replacement string
In fact, the separator "/" can be replaced by 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's much better to use "_" as a separator than "/".
1.2 Using & to represent matching strings
Sometimes you might want to add some characters around or near the matched string.
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 the first occurrence of the search string by default, using/g to replace the search string with all
$ sed ' s/test/mytest/g ' example-----Replace test with mytest in the entire row range. If there is no G tag, only the first matching test of each row is replaced with mytest.
$ sed ' s/^192.168.0.1/&localhost/' example-----& symbol represents the part found in the replacement string. All lines starting with 192.168.0.1 will be replaced with a self-added 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 delimiter, instead of the default "/" delimiter. 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
Get eth0 network card IP address:
650) this.width=650; "title=" clip_image003 "style=" Background:url ("/e/u261/themes/default/images/word.gif") No-repeat center;border:1px solid #ddd; "border=" 0 "alt=" clip_image003 "src="/e/u261/themes/default/images/ Spacer.gif "width=" 557 "height="/>
2. Delete line: D command
Remove all rows that contain "how" from a file
650) this.width=650; "title=" clip_image004 "style=" Background:url ("/e/u261/themes/default/images/word.gif") No-repeat center;border:1px solid #ddd; "border=" 0 "alt=" clip_image004 "src="/e/u261/themes/default/images/ Spacer.gif "width=" 362 "height="/>
Displays the contents of the/etc/passwd and prints the line number while deleting the 2~5
650) this.width=650; "title=" clip_image005 "style=" Background:url ("/e/u261/lang/zh-cn/images/localimage.png") No-repeat center;border:1px solid #ddd; "border=" 0 "alt=" clip_image005 "src="/e/u261/themes/default/images/ Spacer.gif "width=" 435 "height=" 94 "/>
Attached: The NL command is used in the Linux system to calculate the line number of a file. NL can automatically add the output file content to the line number
If you want to delete line 2nd, you can use NL/ETC/PASSWD | Sed ' 2d ' to achieve, as if to delete the 3rd to the last line, it is NL/ETC/PASSWD | Sed ' 3, $d '.
3. Add row: A command (added after the specified line) or I command (added before the specified line)
A can be followed by strings, and the strings appear in a new line
Add a new line to the word "XXXXX" after the second line of/etc/passwd
650) this.width=650; "title=" clip_image007 "style=" Background:url ("/e/u261/lang/zh-cn/images/localimage.png") No-repeat center;border:1px solid #ddd; "border=" 0 "alt=" clip_image007 "src="/e/u261/themes/default/images/ Spacer.gif "width=" "height=" 108 "/>
Add a new line to the word "XXXXX" before the second line of/etc/passwd
650) this.width=650; "title=" clip_image009 "style=" Background:url ("/e/u261/lang/zh-cn/images/localimage.png") No-repeat center;border:1px solid #ddd; "border=" 0 "alt=" clip_image009 "src="/e/u261/themes/default/images/ Spacer.gif "width=" 459 "height=" 94 "/>
If you want to add more than one row at a time, use a backslash \ for adding new rows between each row
650) this.width=650; "title=" clip_image011 "style=" Background:url ("/e/u261/lang/zh-cn/images/localimage.png") No-repeat center;border:1px solid #ddd; "border=" 0 "alt=" clip_image011 "src="/e/u261/themes/default/images/ Spacer.gif "width=" 452 "height=" 119 "/>
4. Replace line: C command
C can be followed by strings, which can replace rows between n1,n2
650) this.width=650; "title=" clip_image013 "style=" Background:url ("/e/u261/lang/zh-cn/images/localimage.png") No-repeat center;border:1px solid #ddd; "border=" 0 "alt=" clip_image013 "src="/e/u261/themes/default/images/ Spacer.gif "width=" 522 "height="/>
5. Print: P command
Sed '/north/p ' datafile output all rows by default, find rows in north to repeat print
Sed–n '/north/p ' datafile suppresses default output, prints only rows found north
nl/etc/passwd | Sed-n ' 5,7p ' lists only the 5th to 7th line of content in the/etc/passwd file
Note: the-i option of SED can directly modify the contents of the file
650) this.width=650; "title=" clip_image014 "style=" Background:url ("/e/u261/themes/default/images/word.gif") No-repeat center;border:1px solid #ddd; "border=" 0 "alt=" clip_image014 "src="/e/u261/themes/default/images/ Spacer.gif "width=" 352 "height="/>
6. Extended:
There are three ways of calling Sed:
L type commands at the command line
L INSERT the SED command into the script file and then call SED
L INSERT the SED command into the script file and make the SED script executable.
A, use the SED command-line format as:
sed [Options] sed command input file.
Remember to add single quotes to the actual command when using the SED command at the command line. SED also allows double quotes.
B. Use the SED script file in the following format:
sed Options - f sed script file input file
C, to use the SED script file with the SED command interpreter in the first line, in the following format:
sed script file [options] input file
Whether you use the shell command line or the script file, sed accepts input from standard input, typically a keyboard or redirection result, if no input file is specified.
The SED options are as follows:
-F,--filer=script-file boot sed script file name
General Examples:
The Test.txt is processed by the SED script, and the contents of the Test.txt file are as follows:
650) this.width=650; "title=" clip_image016 "style=" Background:url ("/e/u261/lang/zh-cn/images/localimage.png") No-repeat center;border:1px solid #ddd; "border=" 0 "alt=" clip_image016 "src="/e/u261/themes/default/images/ Spacer.gif "width=" 466 "height=" "/>"
Create sed script file append.sed, add content to test.txt via sed script, with the following script:
650) this.width=650; "title=" clip_image018 "style=" Background:url ("/e/u261/lang/zh-cn/images/localimage.png") No-repeat center;border:1px solid #ddd; "border=" 0 "alt=" clip_image018 "src="/e/u261/themes/default/images/ Spacer.gif "width=" 236 "height="/>
Save it and add executable permissions: chmod +x append.sed
Run Script append.sed
650) this.width=650; "title=" clip_image020 "style=" Background:url ("/e/u261/themes/default/images/word.gif") No-repeat center;border:1px solid #ddd; "border=" 0 "alt=" clip_image020 "src="/e/u261/themes/default/images/ Spacer.gif "width=" 313 "height="/>
The results appear as follows:
650) this.width=650; "title=" clip_image022 "style=" Background:url ("/e/u261/lang/zh-cn/images/localimage.png") No-repeat center;border:1px solid #ddd; "border=" 0 "alt=" clip_image022 "src="/e/u261/themes/default/images/ Spacer.gif "width=" 475 "height=" "/>"
Now look at its specific features.
The first line is the SED command interpretation line. The script finds the SED in this line to run the command, which is located in/bin.
The second line starts with/company/, which is the starting position for the attach operation. A\ notify SED This is an additional operation, first insert two new rows.
The third to fourth line is the actual text of the attach operation to be added to the copy.
This is only an example of adding new rows to the SED script, and other operations on SED will be extrapolate.
This article is from the "Small White blog" blog, please be sure to keep this source http://xiaobai1981.blog.51cto.com/9649602/1737431
Set Application (i)