The--sed command for Linux commands

Source: Internet
Author: User
Tags modifier line editor

Programming Ability:

Script programming

Grep,sed (stream programmer), awk

SED basic usage:

Sed:stream EDitor

Line Editor (full screen Editor: VI)


SED: Mode space

By default, the original file is not edited, only the data in the pattern space is processed, and then the space after processing is printed to the screen


Usage:

sed [OPTION] ... {Script-only-if-no-other-script} [Input-file] ...

SED + option +{sed script}


sed [option] ' addresscommand ' file ... \ for this (these) file conforms to this specified range of rows to implement the specified command.

-N: Silent mode (does not display the contents of the mode space)

-I: Modify the original file directly

-E script-e script: Multiple scripts can be executed at the same time

-f/path/to/sed_script

Sed-f/path/to/scripts File

-r: Indicates the use of extended regular expressions


Address:


    1. Startline,endline

      Like 1,100.

    2. /regexp/\ Regular expression to specify the pattern

      For example,/^root/\ has root started line

    3. /pattern1/,/pattern2/\ Mode 1, Mode 2

      The first time the line is pattern1 matched to the end of the line, to the first time that the row is pattern2 matched to, all rows in the middle.

    4. linenumber \ Line Number

      $: Last line

    5. StartLine, +n \ Starts from the specified line, and the N line is backward.




Command:

D: Delete the qualifying line;

Cases:

#sed ' 1,2d '/etc/fstab \ Delete/etc/fstab in 1, 2 rows

#sed '/s/d '/etc/fstab \ Delete rows with ' s ' in/etc/fstab

#sed '/^/\/d '/etc/fstab \ Delete the line beginning with the slash (\ is the special character to be used/escaped)


P: Show rows that match the criteria

Cases:

#sed '/^/\/d '/etc/fstab \ Delete the line beginning with the slash (\ is the special character to be used/escaped)

#sed-n '/^/\/p '/etc/fstab \ Displays only lines that begin with a slash

# sed-n '/^#/p '/etc/fstab \ Print lines beginning with #


A \string: Appends a new line after the specified line, with the contents of string

Example: # sed '/s/a \hi '/etc/fstab \ Adds a row after all lines with S.

#sed '/s/a \hi. \nhello. '/etc/fstab \ Adds a row behind all lines with S, Hi, a line hello.

I \string: Adds a new row before the specified line, with the contents of string

R file: Adds the contents of the specified file to the specified line.

Cases:

# sed ' 1r/etc/passwd '/etc/fstab \ Adds the contents of the file passwd behind the first line of the file fstab.


W File: Save the row in the range specified by the address to the specified file

Cases:

#sed '/s/w/tmp/s.txt '/etc/fstab \ Saves the row with s in the Fstab file to the S.txt file under TMP


s/pattern/string/modifier: \ Replaces the pattern found in the file with string (the default is to replace only the first matching string in each line) modifier g: Global substitution I: ignoring character case

Additional: s///can be written as s###, s$$$ \ delimiter can be replaced with special characters

&: The entire string referencing the pattern match

Cases:

#sed ' s/s/s/'/tmp/s.txt \ Replace all s in file s.txt with S

#sed ' s/l. e/&r/' reference. SH \ will file reference. Sh with a character that begins with the E end and adds R

#sed ' s#l\ (. e\) #L \1#g ' reference. SH \ Change the reference character part with a back reference


This article is from the "10858195" blog, please be sure to keep this source http://10868195.blog.51cto.com/10858195/1969459

The--sed command for Linux commands

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.