One Linux command per day: sed command __linux

Source: Internet
Author: User

Sed's handling of the text is powerful, and the SED is very small, with fewer parameters and easier to master, and his way of doing it is a bit like awk. Sed reads files sequentially line by row. It then performs 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 complete all the actions on a line, it reads the next line of the file, and then repeats the process until it completes the file. Notice here that the source file (by default) remains unmodified. Sed reads the entire file by default and modifies each row in it. It is a line of action. I use the SED mainly is to use inside the substitution function, really is very powerful. The following example, in detail, first from the beginning of the replacement, the most commonly used.

Sed-h-N,--quiet,--silent cancel automatic print mode space-e script,--expression= script Add "script" to program run list-F script file,--file= script file Add "script file" to program run List--follow-symlinks directly modify file to follow soft link-i[extension],--in-place[= extension] directly modify file (if specified extension is backup file)-L-N,--line-length=n specify "L" command Line-Wrapping expected length--posix closes all GNU extensions-R,--regexp-extended uses extended regular expression-s in the script,--separate treats the input file as separate files instead of a long continuous input-u,--unbuf Fered reads the least data from the input file, refreshes the output more frequently--help print help and Exits--version output version information and exits

[Zhangy@blackghost mytest]# sed ' s/root/tankzhang/' test |grep tank
Note: Replace the root in the test file with Tankzhang, except only once and terminate the operation in this line, go to the next line


[Zhangy@blackghost mytest]# sed ' s/root/tankzhang/g ' test |grep Zhang
Description: Replace the root of the file test with Tankzhang, note that the G-letter, global abbreviation


[Zhangy@blackghost mytest]# sed-n ' s/root/tankzhang/pg ' test
With-N-PG indicates that only those lines that have been replaced are printed (replace all), and I did not add grep to the example above.


[Zhangy@blackghost mytest]# sed-n ' s/root/tankzhang/p ' test

Add-n p to print only those lines that have been replaced (partially replaced), the example above, I did not add grep

[Zhangy@blackghost mytest]# Cat Test | Sed-ne ' 2,8S/^ZHANG/YING/GP '
In the second line, to line eighth, replace the line beginning with Zhang, replace it with Ying, and display the replaced row

[Zhangy@blackghost mytest]# Cat Test | Sed-n  ' 2,8S/^ZHANG/YING/GP;5,10S#DBUS#GOODBAY#GP '
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 to replace the line at the beginning of Zhang with Ying, between 5 and 10, replacing the Dbus with Goodbay, and showing the replaced row, between the second line and line eighth.

[Zhangy@blackghost mytest]# Cat Test | Sed-ne ' 2,8s/zhang/ying/gp '-ne  ' 5,10S#DBUS#GOODBAY#GP '
This example, like the one above, is just a bit different, that is, e to act as a semicolon, and-e can split multiple 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.