Linux Three Musketeers-sed

Source: Internet
Author: User
Tags control characters processing text

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:

1 sed-h 2-  N,--quiet,--silent    cancels the automatic print mode space 3-  e script,--expression= script   adds "script" to the program's run list 4-  F script file,--file = Script file  Add "script file" to the program's run list 5  --follow-symlinks    directly modify the file following the soft link 6  -i[extension],--in-place[= extension]    directly modify the file ( If you specify an extension on the backup file) 7  -L N,--line-length=n   Specifies the line-of-expected length of the "L" Command 8  --posix  Close all GNU Extensions 9  -R,-- Regexp-extended  using the extended regular expression in a script  ,--separate treats  the input file as separate files instead of a long sequential input-  u,--unbuffered  read minimum data from input file, refresh output more frequently  --help     print Help and exit  --version  output version information and exit

Example exercises:

Test files such as:

1 Root:x:0:0:root:/root:/bin/bash 2 Bin:x:1:1:bin:/bin:/bin/false 3 Daemon:x:2:2:daemon:/sbin:/bin/false 4 mail:x : 8:12:mail:/var/spool/mail:/bin/false 5 Ftp:x:14:11:ftp:/home/ftp:/bin/false 6 &nobody:$:99:99:nobody:/:/bin/ False 7 zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash 8 http:x:33:33::/srv/http:/bin/false 9 Dbus:x:81:81:system Message bus:/:/bin/false10 Hal:x:82:82:hal daemon:/:/bin/false11 mysql:x:89:89::/var/lib/mysql:/bin/false12 aaa:x : 1001:1001::/home/aaa:/bin/bash13 ba:x:1002:1002::/home/zhangy:/bin/bash14 test:x:1003:1003::/home/test:/bin/ bash15 @zhangying:*: 1004:1004::/home/test:/bin/bash16 policykit:x:102:1005:po

One: Replace root in test file with Tankzhang

1 # sed ' 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 zhang2  tankzhang:x:0:0:tankzhang:/tankzhang:/bin/bash3  zhangy:x : 1000:100:,,,:/home/zhangy:/bin/bash4  ba:x:1002:1002::/home/zhangy:/bin/bash5  @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 # sed-n ' s/root/tankzhang/p ' test2  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 # Cat Test | Sed-ne ' 2,8S/^ZHANG/YING/GP ' 2  yingy:x:1000:100:,,,:/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:1000:100:,,,:/home/zhangy:/bin/bash3  Goodbay:x:81: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 ' Test 2  root:x:0:0:root:/root:/bin/bash 3  bin:x:1:1:bin:/bin:/bin/false 4  Daemon:x:2:2:daemon:/sbin:/bin/false 5  mail:x:8:12:mail:/var/spool/mail:/bin/false 6  ftp:x:14:11: Ftp:/home/ftp:/bin/false 7  &nobody:$:99:99:nobody:/:/bin/false 8  Root:x:0:0:root:/root:/bin/bash 9  http:x:33:33::/srv/http:/bin/false10  dbus:x:81:81:system message bus:/:/bin/false11  hal:x:82:82: HAL daemon:/:/bin/false12  mysql:x:89:89::/var/lib/mysql:/bin/false13  aaa:x:1001:1001::/home/aaa:/bin/ bash14  root:x:0:0:root:/root:/bin/bash15  test:x:1003:1003::/home/test:/bin/bash16  root:x:0:0: Root:/root:/bin/bash

Note: Special matches

1 matching numbers don't forget that there is a square bracket outside the brackets. 2 [: Alnum:] alphanumeric [A-Z 0-9] 3 [: Alpha:] 4 [: blank:] Space or Tab 5 [: Cntrl:] Any control character 6 [:d Igit:] Number [0-9] 7 [: GRA Ph:] Any visual characters (no spaces) 8 [: Lower:] lowercase [A-z] 9 [:p rint:] Non-control characters [:p UNCT:] Punctuation character one by one [: space:] space [: Upper:] Uppercase [A-Z]13 [: Xdigit:] Hex Digit [0-9 a-f a-f]

Example A, delete 1, 14 rows

1 $ Sed-e ' 1,14d ' test2  @zhangying:*: 1004:1004::/home/test:/bin/bash3  Policykit:x:102:1005:po

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

1 $ Sed-e ' 4, $d ' test2  root:x:0:0:root:/root:/bin/bash3  bin:x:1:1:bin:/bin:/bin/false4  daemon:x:2:2: Daemon:/sbin:/bin/false

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

1 $ Sed-e '/\ (false\|bash\) $/d ' test2  policykit:x:102:1005:po

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

1 $ Sed-e '/root/,/^test/d ' test2  @zhangying:*: 1004:1004::/home/test:/bin/bash3  Policykit:x:102:1005:po

Linux Three Musketeers-sed

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.