sed :streameditor, meaning stream editor, is a common command-line program for Unix-like operating systems. sed is used to convert text inside a document or string through a series of editing commands to another format output;sed is typically used to match the text of one or more regular expressions to handle.
how SED works:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/73/0E/wKiom1Xyq_fDiqq1AAHtNSfjAAU021.jpg "title=" 1.jpg " alt= "Wkiom1xyq_fdiqq1aahtnsfjaau021.jpg"/>
The SED maintains a buffer space of two data: Mode space (patternspace) and reserved space (hold spaces), and the initial environment is empty;
1, when the sed command to a text file processing, read the text file by line to match the address, the matching line is placed in the pattern space;
2. the edit command of SED will edit the contents of the pattern space, then put the edited content in the reserved space;
3, continue to read the next line to perform the above operation, until the last line;
Regular Expressions : The address pattern matching of the SED command supports regular expressions , just likethe grep command previously learned ;
BRE:
character matching:. , [],[^]
number of matches:*,\? ,\+,\{m,n\}
Location anchoring:^,$,\<,\>
grouped references:\ (\),\1,,2 ...
Ere:
number of matches:*,? ,+,{m,n}
Group: ()
or:|
SED command format:
# sed [option] ' address • Edit Command ' FILE
Sed common options:
-N: Silent mode, does not display the contents of the mode space-R: supports the use of extended regular expressions;-I: Modifies the original file;-e:sed-e ""-E ""-e "," sed "{COM1; COM2; COM3} "; means using more than one command-f/path/to/sed_script: reads the processing script from the specified text;
Edit command: command can be added before ! Take counter
P: print; d: Delete, I \test: Insert test content above the line that matches to, a \test: Insert test content below the line that matches, C \test: Modify the test content to match; R/path/to/some_file: Insert the specified file underneath the matched content W/path/to/some_file: Save the qualifying row to the specified file; =: Displays the line number of the qualifying line; S/test1/test2/gi,[email protected]@[email protected]: content substitution
Address:
Line range:
Sratr_line,end_line: Start line , end line
/patten1/,/patten2/: The first line that is matched by the pattern1, to the end of the line that is first matched to the row by the pattern2;
Specific line:
Line
/pattern/
No address: Full text
Example exercises:
1.copy The/etc/inittab file to the/tmp/ directory and replace the "Id:3:initdefault" line in the/tmp/inittab file with the number 5;
# cp/etc/inittab/tmp/# sed ' s/id:3:initdefault/id:5:initdefault/'/tmp/inittab
2, copy the/etc/rc.d/init.d/functions file to /tmp directory, delete The blank line of the/tmp/functions file;
# cp/etc/rc.d/init.d/functions/tmp/# sed '/^[[:space:]]*$/d '/tmp/functions
3.copy The/boot/grub/grub.conf file to the/tmp/ directory and delete the blank characters from the beginning of the/tmp/grub.conf file;
# cp/boot/grub/grub.conf/tmp/# sed ' [email protected]^[[:space:]]\[email protected]@ '/tmp/grub.conf
4. Echo a path to sed, take out its path name via sed, e.g. echo "etc/sysconfig/network-scripts/" | Sed, return path name /etc/sysconfig/;
# echo "/etc/sysconfig/network-scripts/" | Sed ' [Email protected][^/]\+/\[email protected]@ '
This article is from the "Pony Learning Record" blog, make sure to keep this source http://masachencer.blog.51cto.com/8683770/1693919
Text processing the Three Musketeers ' sed basic usage