Sed very powerful text manipulation commands

Source: Internet
Author: User

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.

One, test the file, the same as when you speak awk.
Sed-h-n,--quiet,--silent cancels the automatic print mode space-e script,--expression= script adds "script" to the program's run list-F script file,--file= script file adds "script file" to the program's run list- -follow-symlinks follow the soft link-i[extension] directly when modifying a file,--in-place[= extension] directly modifies the file (if you specify the extension for the backup file)-L N,--line-length=n specifies the line length of the "l" command --posix Close all GNU extension-R,--regexp-extended uses the extended regular expression-s in the script,--separate treats the input file as separate files instead of a long continuous input-u,--unbuffered from the input text Data, more frequent refresh output--help print help and exit--version output version information and exit//the contents of the test file below Root:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin :/bin/falsedaemon:x:2:2:daemon:/sbin:/bin/falsemail:x:8:12:mail:/var/spool/mail:/bin/falseftp:x:14:11:ftp:/ home/ftp:/bin/false&nobody:$:99:99:nobody:/:/bin/falsezhangy:x:1000:100:,,,:/home/zhangy:/bin/bashhttp:x : 33:33::/srv/http:/bin/falsedbus:x:81:81:system message Bus:/:/bin/falsehal:x:82:82:hal daemon:/:/bin/falsemysql: x:89:89::/var/lib/mysql:/bin/falseaaa:x:1001:1001::/home/aaa:/bin/bashba:x:1002:1002::/home/zhangy:/bin/ Bashtest:x:1003:1003::/home/test:/bin/bash@zhangying:*:1004:1004::/home/test:/bin/bAshpolicykit:x:102:1005:po 
Two, example 1, replacement function
[[Email protected] mytest]# sed ' s/root/tankzhang/' test |grep tank Tankzhang:x:0:0:root:/root:/bin/bash

In this example, replace the root of the test file with Tankzhang, just replace and terminate the operation in this row, and go to the next line

[Email protected] mytest]# sed ' s/root/tankzhang/g ' test |grep Zhang Tankzhang:x:0:0:tankzhang:/tankzhang:/bin/bash zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash ba:x:1002:1002::/home/zhangy:/bin/bash @zhangying:*: 1004:1004::/ Home/test:/bin/bash

In this example, replace the root of the file test with Tankzhang, notice the letter G, the global abbreviation

[Email protected] mytest]# sed-n ' s/root/tankzhang/p ' test Tankzhang:x:0:0:root:/root:/bin/bash

Adding-n p indicates that only those lines that have substituted (partial substitution) are printed, the above example, I did not add grep

[Email protected] mytest]# sed-n ' s/root/tankzhang/pg ' test Tankzhang:x:0:0:tankzhang:/tankzhang:/bin/bash

Adding the-N PG indicates that only those lines that have replaced are printed (replace All), the above example, I did not add grep

[email protected] mytest]# Cat Test | Sed-ne ' 2,8S/^ZHANG/YING/GP ' yingy:x:1000:100:,,,:/home/zhangy:/bin/bash

In the second row, between lines eighth, replace the lines that begin with Zhang, replace them with Ying, and display the substituted rows

[email protected] mytest]# Cat Test | Sed-n  ' 2,8s/^zhang/ying/gp;5,10s#dbus#goodbay#gp ' yingy:x:1000:100:,,,:/home/zhangy:/bin/bash goodbay:x : 81:81:system Message Bus:/:/bin/false

When more than one command is to be executed, 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

[email protected] mytest]# Cat Test | Sed-ne ' 2,8s/zhang/ying/gp '-ne  ' 5,10s#dbus#goodbay#gp ' yingy:x:1000:100:,,,:/home/yingy:/bin/bash goodbay:x : 81:81:system Message Bus:/:/bin/false

This example, like the one above, is just a little different, that is,-e to act as a semicolon, and-e can split multiple commands.

[Email protected] mytest]# sed-ne ' 2,8s/^\ (zhangy\)/\1ing/gp ' test zhangying:x:1000:100:,,,:/home/zhangy:/bin/bash
The use of regular, in the sed inside the parentheses to add the \, otherwise it will be an error.
[Email protected] mytest]# sed-ne ' 2,15s/zhang/&ying/gp ' test zhangyingy:x:1000:100:,,,:/home/zhangyingy:/bin/ Bash Ba:x:1002:1002::/home/zhangyingy:/bin/bash @zhangyingying:*: 1004:1004::/home/test:/bin/bash
And the use of the string after the found, and after the string, Zhang added a ying
[[email protected] mytest]# sed-ne '/^zhang/,/po/s/zhang/ying/gp ' test yingy:x:1000:100:,,,:/home/yingy:/bin/bash BA: X:1002:1002::/home/yingy:/bin/bash @yingying:*: 1004:1004::/home/test:/bin/bash

The above example says that at the beginning of the line starting with Zhang, to the end of the line matching the PO, between them to replace

[[Email protected] mytest]$ sed  '/root/{n;s/bin/tank/} ' test Root:x:0:0:root:/root:/bin/bash Tank:x:1:1:bin:/bin :/bin/false
n; here n is the abbreviation for next, and after finding the root line, replace the bin in the next line with tank
[Email protected] mytest]$ sed-e ' 1,2y/root/root/' Test Root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/bin/ False
The role of Y is to capitalize the matched characters, but the replacement and the replacement characters are the same length
[Email protected] mytest]$ sed-e '/root/h '-e ' $G ' Test ... ..... ..... ..... ..... ..... ....... ........................ Ba:x:1002:1002::/home/zhangy:/bin/bash test:x:1003:1003::/home/test:/bin/bash @zhangying:*: 1004:1004::/home/test :/bin/bashroot:x:0:0:root:/root:/bin/bash

In this example, the function of H is to put the found row into a buffer, and G is to put the contents of the buffer in the last line.

[Email protected] mytest]$ sed-e '/root/h '-e '/zhangy/g ' Test Root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/bin /false Daemon:x:2:2:daemon:/sbin:/bin/false Mail:x:8:12:mail:/var/spool/mail:/bin/false ftp:x:14:11:ftp:/home/ftp :/bin/false &nobody:$:99:99:nobody:/:/bin/false Root:x:0:0:root:/root:/bin/bash http:x:33:33::/srv/http:/bin/ False Dbus:x:81:81:system message Bus:/:/bin/false Hal:x:82:82:hal daemon:/:/bin/false mysql:x:89:89::/var/lib/mysql :/bin/false aaa:x:1001:1001::/home/aaa:/bin/bash Root:x:0:0:root:/root:/bin/bash test:x:1003:1003::/home/test:/ Bin/bash Root:x:0:0:root:/root:/bin/bash
Line substitution, replacing rows that match zhangy with a row that matches the root
[Email protected] mytest]$ sed-e ' s/bin/tank/g;3q ' Test Root:x:0:0:root:/root:/tank/bash Tank:x:1:1:tank:/tank:/tank /false Daemon:x:2:2:daemon:/stank:/tank/false
3q means to the third row, exit
[Email protected] mytest]# sed-ne ' 2,15s/zhangy.*[[:d igit:]]/=======/gp '  test =======:,,,:/home/zhangy:/bin/ Bash @=======::/home/test:/bin/bash
Match numbers don't forget that there is a bracket outside the brackets.

[: Alnum:] alpha-numeric [A-Z 0-9]
[: Alpha:] letter [A-z]
[: Blank:] Space or TAB key
[: Cntrl:] any control character
[:d Igit:] number [0-9]
[: Graph:] Any visible character (no spaces)
[: Lower:] lowercase [A-z]
[:p rint:] Non-control characters
[:p UNCT:] punctuation character
[: Space:] Space
[: Upper:] uppercase [A-z]
[: xdigit:] hex digit [0-9 a-f a-f]

Two, delete

In fact, I think, delete in fact, the root of the same, is to find out to carry out the operation, the process will be involved in some rules.

[Email protected] test]$ sed-e ' 1,14d ' Test @zhangying:*: 1004:1004::/home/test:/bin/bash policykit:x:102:1005:po
Delete 1, 14 rows
[[email protected] mytest]$ sed-e ' 4, $d ' Test Root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/bin/false daemon:x : 2:2:daemon:/sbin:/bin/false
Delete the line after 4, including the 4th line, and take $ as the maximum number of rows. Ha ha
[Email protected] mytest]$ sed-e '/\ (false\|bash\) $/d ' Test Policykit:x:102:1005:po
Delete the line that includes false, or include the line of bash, and don't forget to add \
[Email protected] mytest]$ sed-e '/root/,/^test/d ' Test @zhangying:*: 1004:1004::/home/test:/bin/bash policykit:x : 102:1005:po
Delete the line from the matching root to match the line starting with test, the middle row three, read, append, insert File
[Email protected] mytest]$ sed-e '/^root/r test2 ' test root:x:0:0:root:/root:/bin/bash =============-------------++++ +++++++++ Bin:x:1:1:bin:/bin:/bin/false Daemon:x:2:2:daemon:/sbin:/bin/false
Reads the contents of the Test2 and writes it to the bottom of the matching line
[[Email protected] mytest]$ sed '/[[:d igit:]]/w test2 ' test
The line that matches the number is written to the Test2
[[Email protected] mytest]$ sed  '/root/a\\ ===aaaa==== ' test Root:x:0:0:root:/root:/bin/bash ===aaaa==== bin:x:1:1 : Bin:/bin:/bin/false
The thing that will be inserted, insert below the matching line,
[[Email protected] mytest]$ sed '/^daemon/i\\================= ' test Root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/ Bin:/bin/false ================= Daemon:x:2:2:daemon:/sbin:/bin/false Mail:x:8:12:mail:/var/spool/mail:/bin/false
Exactly the root a instead, the thing that will be inserted, insert the above four into the matching line, call the command file
[[Email protected] mytest]$ sed-  f cmd Test |awk ' {print $;} ' Tank:x:0:0:tank:/tank:/tank/bash tank:x:1:1:tank:/ Tank:/tank/false Ba:x:1002:1002::/home/zhangy:/tank/bash Test:x:1003:1003::/home/test:/tank/bash @zhangying:* : 1004:1004::/home/test:/tank/bash
    • This article is from: Linux Tutorial Network

Sed very powerful text manipulation commands

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.