Use of sed for example--unix Shell

Source: Internet
Author: User

Sed and awk are powerful text filtering tools in the UNIX environment.


With SED and awk, it is easy to edit local files, and it is easier to use regular expressions to filter out the output of other commands.


SED represents the stream Editor, which means that sed is a flow-oriented mechanism. All inputs provided for the SED command are sent to STDOUT after being processed by SED.

The SED command does not change the input text.


Common sed format:

I. Implementation of a single SED

Sed reads each row of data and executes an action.

The syntax is:

Sed ' script ' files

Note that the script is wrapped in inverted single quotes, and the reverse single quotation mark is the ~ key character of the same key.


Script is one or more commands in the following format:

/pattern/action


Pattern is a regular expression

ACTION:P Print the data rows being processed

d Delete the data rows being processed

S/pattern1/pattern2/replaces the first expression with the second regular formula,/pattern/s/pattern1/pattern2/the original command in this case, omitting the s/pattern1/pattern2/


Ii. execution of multiple SED commands

Execute multiple SED commands with the following command:

Sed-e ' Command1 '-e ' command2 '-e ' command3 ' files


A common example is to use SED to determine a user's user id:

ID | Sed-e s/uid=//g-e s/\ (. *//g

ID Output:

Uid=500 (David) gid=500 (David)

First command:

-e s/uid=//g means replacing uuid= with null

-e s/\ (. *//g indicates that all characters from the first parenthesis are replaced with empty


Thus, the value of the UID is obtained: 500



Related Article

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.