How to use the SED command in Linux

Source: Internet
Author: User
Tags linux

Sed is an online editor that handles a single line of content at a time. When processing, the currently processed rows are stored in a temporary buffer, known as pattern spaces, followed by the SED command to handle the contents of the buffer, and the contents of the buffer are sent to the screen after processing is complete. The next line is processed so that it repeats until the end of the file. The contents of the file do not change unless you use redirected storage output.

Basic commands for SED:

1. Replace: s command

1.1 Basic usage

such as: sed ' s/day/night/' <old >new

This example replaces the first occurrence of day in the file old with night, outputting the result to the file new

S "Replace" command

/.. /.. /split character (Delimiter)

Day search string

Night Replacement string

In fact, the separator "/" can be replaced with other symbols, such as ",", "|" and so on.

such as: sed ' s/\/usr\/local\/bin/\/common\/bin/' <old >new

Equivalent to sed ' s_/usr/local/bin_/common/bin_ ' <old >new

Obviously, it is much better to use "_" as a separator than "/".

1.2 Matching string with &

Sometimes you might want to add some characters around or around a string that matches.

such as: sed ' s/abc/(ABC)/' <old >new

This example adds parentheses around the found ABC.

This example can also be written as Sed ' s/abc/(&)/' <old >new

The following are more complex examples:

Sed ' s/[a-z]*/(&)/' <old >new

SED replaces only the first occurrence of the search string by default, using/g to replace the search string for all

$ sed ' s/test/mytest/g ' example-----Replace test with mytest across the entire line. If there is no G-tag, only the first matching test for each row is replaced with mytest.

$ sed ' s/^192.168.0.1/&localhost/' example-----& symbol represents the part found in the replacement string. All rows beginning with 192.168.0.1 are replaced with localhost, which becomes 192.168.0.1localhost.

$ sed ' s#10#100#g ' example-----no matter what character, followed by the S command is considered a new delimiter, so, "#" Here is the separator, instead of the default "/" separator. means to replace all 10 with 100.

If you need to make multiple modifications to the same file or row, you can use the "-e" option

Obtain eth0 network card IP address:

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.