The SED of the Three Musketeers of Linux

Source: Internet
Author: User

Sed

SED is very powerful in processing text, and sed is very small, with few parameters, easy to master, and his way of operation is a bit like the root of awk. Sed reads the file sequentially by line. It then executes all the actions specified for the row and displays it after the modification of the request is completed, or it can be stored in a file. After you finish all the actions on a line, it reads the next line of the file, and then repeats the process until it finishes the file. Note here that the source files (by default) remain unmodified. Sed reads the entire file by default and modifies each row in it. To be blunt is a row of operations. I use sed mainly with the inside of the replacement function, really very powerful. The following example, in detail, first from the replacement, the most commonly used.

Parameters:

1SED-h2-N,--quiet,--Silent canceling automatic print mode space3-E script,--expression=script adds "script" to the program's run list4-F script file,--file=script File Add "script file" to the program's run list5--follow-symlinks Follow soft links when modifying files directly6-i[extension],---inch-place[=extension] Directly modify the file (if you specify the extension to back up the file)7-L-N,--line-length=N Specifies the expected length of the newline for the "L" command8--POSIX shuts down all GNU extensions9-R,--regexp-extended using an extended regular expression in a scriptTen-S,--separate the input file as individual files instead of a long continuous input One-U,--unbuffered reads the least data from the input file and refreshes the output more frequently A--Help print assistance and exit ---version output version information and exit

Example exercises:

Test files such as:

1ROOT:X:0:0: root:/root:/bin/Bash2BIN:X:1:1: bin:/bin:/bin/false3DAEMON:X:2:2:d aemon:/sbin:/bin/false4MAIL:X:8: A: mail:/var/spool/mail:/bin/false5FTP:X: -: One: ftp:/home/ftp:/bin/false6&nobody:$: About: About: nobody:/:/bin/false7ZHANGY:X: +: -:,,,:/home/zhangy:/bin/Bash8HTTP:X: -: -::/srv/http:/bin/false9DBUS:X:Bayi:Bayi: System Message bus:/:/bin/falseTenHAL:X: the: the: HAL daemon:/:/bin/false OneMYSQL:X: the: the::/var/lib/mysql:/bin/false AAAA:X:1001:1001::/home/aaa:/bin/Bash -BA:X:1002:1002::/home/zhangy:/bin/Bash -TEST:X:1003:1003::/home/test:/bin/Bash the@zhangying:*:1004:1004::/home/test:/bin/Bash -POLICYKIT:X:102:1005:P o

One: Replace root in test file with Tankzhang

1 ' s/root/tankzhang/ ' test

Second: Replace the root of the file test with Tankzhang, please note that the letter G

 1  # sed  " s/root/tankzhang/g   Test | grep Zhang  2  tankzhang:x:0 : 0 : Tankzhang:/tankzhang:/bin/bash  3  zhangy:x:1000 :   :,,,:/home/zhangy:/bin/bash  4  Ba:x:1002 : 1002 ::/home/zhangy:/bin/< Span style= "COLOR: #000000" >bash  5  @zhangying:*: 1004 : 1004 ::/home/test:/bin/bash 

Three: Add-n p to only print those occurrences of the replacement line (partial substitution), the above example, I did not add grep

1 ' s/root/tankzhang/p ' Test 2  Tankzhang:x:0:0: Root:/root:/bin/bash

Four: In the second row, between line eighth, replace the line with the beginning of Zhang, replace with Ying, and display the replaced line

1 ' 2,8S/^ZHANG/YING/GP ' 2  Yingy:x:+:,,,:/home/zhangy:/bin/bash

Five: When there are more than one command to execute, it can be separated by semicolons, and the delimiter can be customized by default. The above example means, in the second line, between lines eighth, replace the line with the beginning of Zhang, replace with Ying, in 5, to 10, replace Dbus with Goodbay, and display the substituted line

1 # Cat Test | Sed-n  '2,8s/^zhang/ying/gp;5,10s#dbus#goodbay#gp'2  yingy:x:  +:,,,:/home/zhangy:/bin/bash3  goodbay:x:bayi:81 : System message bus:/:/bin/false

Six:, line substitution, with matching root row, to replace the line matching zhangy

1$ sed-e'/root/h'-E'/zhangy/g'Test2ROOT:X:0:0: root:/root:/bin/Bash3BIN:X:1:1: bin:/bin:/bin/false4DAEMON:X:2:2:d aemon:/sbin:/bin/false5MAIL:X:8: A: mail:/var/spool/mail:/bin/false6FTP:X: -: One: ftp:/home/ftp:/bin/false7&nobody:$: About: About: nobody:/:/bin/false8ROOT:X:0:0: root:/root:/bin/Bash9HTTP:X: -: -::/srv/http:/bin/falseTenDBUS:X:Bayi:Bayi: System Message bus:/:/bin/false OneHAL:X: the: the: HAL daemon:/:/bin/false AMYSQL:X: the: the::/var/lib/mysql:/bin/false -AAA:X:1001:1001::/home/aaa:/bin/Bash -ROOT:X:0:0: root:/root:/bin/Bash theTEST:X:1003:1003::/home/test:/bin/Bash -ROOT:X:0:0: Root:/root:/bin/bash

Note: Special matches

1 match numbers don't forget that there is a bracket outside the brackets. 2[: Alnum:] alpha-numeric [A-Z0-9]3[: Alpha:] letter [A-Z-Z]4 [: blank:] Space or TAB key5 [: Cntrl:] any control character6[:d Igit:] Number [0-9]7 [: Graph:] Any visible character (no spaces)8[: Lower:] lowercase [aZ]9 [:p rint:] Non-control charactersTen [:p unct:] Punctuation character One [: Space:] Space A[: Upper:] Uppercase [A-Z] -[: xdigit:] hex Digit [0-9A-f A-f]

Example A, delete 1, 14 rows

1 ' 1,14d ' Test 2  @zhangying:*:1004:1004::/home/test:/bin/bash3  policykit:x:102 :1005:P o

Example B, delete the line after 4, including the 4th row, the $ as the maximum number of rows.

1 ' 4, $d ' Test 2  Root:x:0:0: root:/root:/bin/bash3  bin:x:1:1: bin:/bin:/bin/false4  daemon:x:2:2:d aemon:/sbin:/bin/  False

Example C, delete the line that includes false, or include the row of bash, and don't forget to add \

1 ' /\ (false\|bash\) $/d ' Test 2  POLICYKIT:X:102:1005:P o

Example D, delete the row from the matching root to the line that matches the beginning of test, the middle row

1 ' /root/,/^test/d ' Test 2  @zhangying:*:1004:1004::/home/test:/bin/bash3  policykit:x: 102:1005:P o

The SED of the Three Musketeers of Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.