Description
Sed stream editor for filtering and converting text
You can use regular matching, insert and delete text, and so on.
When SED is processed, one line is processed at a time, each time the current processing is stored in a temporary buffer, the output buffer content is processed to the screen, and the next line is read into the buffer, so repeat until the last line is processed.
Usage:
sed [OPTION] ... {Script-only-if-no-other-script} [Input-file] ...
sed [options] ... Action File
Parameters:
-N Quiet mode, cancel default output
-E performs SED action editing directly on the command column
-F Read script file within SED command processing file
-R SED action supports extended regular expression (default is only the underlying regular)
-I directly modify the contents of the file (use System files sparingly during the experiment)
-U loads a small amount of data from the input file and refreshes more output buffers
--HELP Display Help information and exit
--version output version information and exit
Command:
: Label is the tag of the B and T commands, which can be used for jumping processing, and the name may be randomly taken (label)
= Print the current line number
A\ Append, append text to the next line in the current row
I\ Insert, insert text on the previous line of the current row
Q Exit, same as Q
B Label branch to label, if the label is omitted, then branch to the end of the script.
T label if a successful substitution has been made from the last time, the input line is read, and the final T or T command is then branched to the label, and if omitted then branched to the end.
T label if no s///from the last successful replacement input line is read, and from the final T or T command, then branch to the label. If omitted, branches to the end.
C \ replaces the selected line with all embedded text, preceded by a backslash.
d Delete the selected row
D Delete the first line of the stencil fast
H h Copy or append the contents of the module section to the buffer
G G gets the contents of the memory buffer and replaces or appends the text to the current module section
X-swap retention and pattern space content
L LIST the moving forward in a visually explicit form
n N reads or attaches the next line of input to the pattern space
P print rows in the current module section
P Print the first row of MO plate
s///Replace matching characters, you can use regular
W writes the current mode space to the file
W writes the first line of the current mode space to the file
! Indicates that the following command has a function on all rows that are not selected
# extend annotations before the next line break
sed replacement tokens :
G means full in-line replacement
P indicates that the line is printed
W means writing a line to a file
X represents the exchange of text in the Mo Plate and the text in the buffer
Y means translating a character into another character.
\1 substring matching tag
& Matched String Markers
sed meta character set :
^ Match Line start
$ Match Line End
. Any character that matches a non-line break
* Match 0 or more characters to match all modules are one or more spaces immediately following the SED line
[] matches a character within a specified range
[^] matches a character that is no longer specified in the range
\(.. \) match substring, save matching characters such as s/\ (love\) able/\1rs loveable is replaced with lovers
& Save Search characters to replace other characters, such as s/love/**&**/love this into **love**
\< matches the beginning of a word, such as a/\<love/match with a line of words beginning with love
\> matches the end of a word, such as/love\>/matches a line containing a word ending in love
X\{m\} repeats characters x,m times, such as/0{5\}/matches a row containing 5 0
X\{m,\} repeat character x, at least m times, such as/0\{5,\}/matches at least 5 rows of 0
X\{m,n\} repeats the character x, at least m times, not more than n times, such as/0\{5,10\}/matches 5~10 0 rows
Instance:
Replace operation: s command
Replace a string in text
[[Email protected] ~]# sed ' s/aa/99/' aa.txt 123 345 aa bb cc DD BB Eebb 123 333 444 333 222 bb
The-n option and the P option work together to indicate that only the rows that have changed are printed
[[email protected] ~]# sed-n ' s/aa/www.baidu.com/p ' aa.txt www.baidu.com 123 345 aa bb cc DD BB ee
Directly edit File Option-I, matching each line of the file matches the content
[[Email protected] ~] #sed-i ' s/aa/888/g ' aa.txt #全面替换标记g [[email protected] ~]# Cat aa.txt 888 123 345 888 BB CC DD BB Eebb 123 333 444 333 222 bb
You can use/ng when you need to start replacing from the nth-out match:
[Email protected] ~]# echo Ababababababab | Sed ' s/ab/ab/2g ' ababababababab[[email protected] ~]# echo Ababababababab | Sed ' s/ab/ab/3g ' ababababababab[[email protected] ~]# echo Ababababababab | Sed ' s/ab/ab/4g ' ababababababab[[email protected] ~]# echo Ababababababab | Sed ' s/ab/ab/5g ' ababababababab
In the above command/in SED as a delimiter, you can also use any delimiter, for example, using the # number to convert the output to uppercase, where u in the meta-character character is uppercase large meaning,& represents the matched characters.
[Email protected] ~]# echo ABCD |sed ' s#[a-z]#\u& #g ' ABCD
Delete operation: D command
Delete blank line sed '/^$/d ' file delete the 2nd line of sed ' 2d ' file delete the 2nd line to the end of all lines sed ' 2, $d ' file delete the last line of sed ' $d ' file11 Delete all the beginning of the file in the line sed '/^ test/d ' File
Matched string Markers &
The regular expression \w+ matches each word, using [&] to replace it,& the word that was previously matched to
[[email protected] ~]# echo a b c d |sed ' s/\w\+/[&]/g ' [a] [b] [C] [d]
& represents the previously matched content and then adds what needs to be replaced later:
[Email protected] ~]# echo "202.106.0.20" |sed ' s/202.106.0.20/&dns/g ' 202.106.0.20DNS
SUBSTRING matching tag \1
Matches a part of a given style, the first uses \1 the second uses \2, the following is the Intercept IP and MAC address
[Email protected] ~]# ifconfig eth0|sed-n ' s/^.*addr:\ (. *\). * Mask:\ (. *\) $/ip:\1 mac:\2/gp ' ip:10.0.0.4 mac:255.255.25 5.0
The content within the first () of the command matches \1, the second () Big content matches \2, and so on
For example: [a-z]+ means that the + that follows any character is more than one meaning, where you use the-r option to use an extended regular expression, you do not need to escape the special characters.
[[email protected] ~]# echo "AAA BBB" |sed-r ' s/([a-z]+) ([a-z]+]/\2 \1/' BBB AAA
Reference
An SED expression can be referenced using single quotation marks, but if the expression contains a variable string inside it, you need to use double quotation marks
[Email protected] ~]# Aa=mysql;echo "This is SQL Server" |sed ' s/sqlserver/$aa/' This is $AA [[email protected] ~]# aa=mysql; echo "This is SQL Server" |sed "s/sqlserver/$aa/" This is MySQL
Range of selected rows:, comma
All lines within the range defined by the template BBB and DDD are printed
[Email protected] ~]# echo-e "aaa\nbbb\nccc\nddd" |sed-n '/bbb/,/ddd/p ' bbbcccddd
Printing from line 2nd to the first contains all lines that begin with the CCC, including the CCC:
[Email protected] ~]# echo-e "aaa\nbbb\nccc\nddd" |sed-n ' 2,/^ccc/p ' BBBCCC
For lines between AAA and CCC, append www.com at the end of each line
[Email protected] ~]# echo-e "aaa\nbbb\nccc\nddd" |sed '/aaa/,/ccc/s/$/www.com/' aaawww.combbbwww.comcccwww.comddd
Multi-point editing: E command
[Email protected] ~]# echo-e "aaa\nbbb\nccc\nddd\neee" |sed-e ' 1,3d '-e ' s/eee/www/' dddwww
The first command of the SED expression above deletes 1 to 3 rows, the second command replaces www with EEE, the order of execution affects the result, and if all two commands are replacements, the first command affects the result of the second substitution command.
And-e equivalent command is –expression
[Email protected] ~]# echo-e "aaa\nbbb\nccc\nddd\neee" |sed--expression ' s/aaa/mmm/'--expression ' s/eee/www/' Mmmbbbcccdddwww
Read from File: R command
The contents of file are read in and displayed after the line that matches the AAA, and if multiple rows are matched, the contents of file will be displayed after all matching rows sed '/aaa/r filename
[Email protected] ~]# echo-e "aaa\nbbb\naaa\nddd\neee" |sed '/aaa/r aa.txt ' Aaawww.orgblog.combbbaaawww.orgblog.comdddeee
Write from File: w command
Writes a matching line to a file, paying attention to overwriting the original file contents
[Email protected] ~]# echo-e "aaa\nbbb\naaa\nddd\neee" |sed-n '/aaa/w aa.txt ' [[email protected] ~]# cat Aa.txt aaaaaa
Append: A\ command
Append www.gun.org to the line starting with AAA
[Email protected] ~]# echo-e "aaa\nbbb\naaa\nddd\neee" |sed '/^aaa/a\www.gun.org ' Aaawww.gun.orgbbbaaawww.gun.orgdddeee
Append www,centos.org after line 2nd of the file
[Email protected] ~]# sed-i ' 2a\www.centos.org ' aa.txt [[email protected] ~]# cat Aa.txt aaaaaawww.centos.org
Insert: I\ command
Insert content in front of a matching line
[Email protected] ~]# echo-e "aaa\nbbb\naaa\nddd\neee" |sed '/^aaa/i\www.gun.org ' Www.gun.orgaaabbbwww.gun.orgaaadddeee
Next n/n command
: Label stores a label position, uses N to append the next line of text to this space, there are two lines of text (AA$BB), and then replaces the newline character (\ n) with the s/\n//p substitution function, and the output aabb,b assigns a tag to the label when it is BB. The statement is then executed line by row at the end of the line.
[email protected] ~]# cat a.txt aabbcc[[email protected]jy ~]# sed ': label; N;s/\n//p;b label ' A.txt AABBAABBCCAABBCC
Deform: Y command
Turn all ABCDE in 1~10 into uppercase, note that regular expression metacharacters cannot use this command
[Email protected] ~]# echo-e "aaa\nbbb\naaa\nddd\neee" |sed ' 1,4y/abcde/abcde/' aaabbbaaadddeee
Exit the Q command
After you finish printing line 10th, exit SED
Sed ' 10q ' file
Print odd or even rows
Method 1, use the n command to print the next line
Print odd lines: [[email protected] ~]# echo-e "aaa\nbbb\naaa\nddd\neee" |sed-n ' p;n ' aaaaaaeee print even lines: [[email protected] ~]# ECHO-E " Aaa\nbbb\naaa\nddd\neee "|sed-n ' n;p ' bbbddd
Method 2, specifies the number of lines to print from the first line, followed by a few lines of the specified interval
[Email protected] ~]# echo-e "aaa\nbbb\naaa\nddd\neee" |sed-n ' 1~2p ' aaaaaaeee[[email protected] ~]# echo-e "aaa\nbbb\na Aa\nddd\neee "|sed-n ' 2~2p ' bbbddd
Print the next line matching a string
[Email protected] ~]# echo-e "aaa\nbbb\naaa\nddd\neee" |sed-n '/ddd/{n;p} ' eee
Use B to jump, when the judgment character is AAA B jumps to X, here: x is the defined label, after the tag is the action of execution. Match the character AA jumps to X to perform the action, add tabs and yes after AA, if not for AA do not jump directly behind the s/$/\tyes/, add tabs and yes later.
[Email protected] ~]# echo-e "aaa\nbbb\naaa\nddd\neee" |sed '/^aaa/bx;s/$/\tno/;b;:x;s/$/\tyes/' Aaayesbbbnoaaayesdddnoeeeno use t do jump, if there is a successful replacement is read into the last T or T command, here still do not understand, understand the great God want message to inform. [Email protected] ~]# echo-e "aaa\nbbb\naaa\nddd\neee" |sed '/^aaa/tx;s/$/\tno/;t;:x;s/$/\tyes/' Aaanobbbnoaaanodddnoeeeno use t do jump, if there is a successful replacement is read into the last T or T command, use the reverse jump, here or not very clear, understand the big God want message to inform. [Email protected] ~]# echo-e "aaa\nbbb\naaa\nddd\neee" |sed '/^aaa/tx;s/$/\tno/; T;:x;s/$/\tyes/' Aaayesbbbnoyesaaayesdddnoyeseeenoyes
Analyze multi-line log take IP and MAC addresses and do statistical examples:
Grep-we ' ^lease|hardware ' $patha/dhcpd.leases|sed ': x; N;s/{\n//;b x ' |sed-n "S/;$//GP" |awk ' Begin{print "IP" \t\t "" MAC "} {print $" \ T "$ $" > $patha/ip_table.txtcat $patha/ Ip_table.txt | Wc-l > $patha/aa.txt
Post-Supplement ....
This article is from the "Gen Y Rookie" blog, so be sure to keep this source http://zhangxinqi.blog.51cto.com/9668428/1921804
Linux Command summary: SED