sed command in Linux

Source: Internet
Author: User
Tags first string modifier

SED basic usage:

Sed:stream Editor Editor, unlike the VI editor, the VI editor is a full-screen editor.

SED: Mode space

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


sed [options] ' addresscommand ' file ...

-N: Silent mode, content not in the default display 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

Notes on Address:

    1. Startline,endline such as 1,100 $: last line

    2. /regexp//^root/

    3. /pattern1/,/pattern2/the first line that is matched by pattern1 to the end of the line that was first matched to the pattern2, all rows in the middle

    4. LineNumber: The specified row

    5. Startline,+n starts from StartLine and is backward N rows.

A description of the command:

D: Delete rows that match the criteria

P: Show rows that match the criteria

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

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 qualifying line

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

s/pattern/string/modifier: Find and Replace, default replaces only the first string in each line that is matched to the pattern

Modifier: G: Global substitution I: ignore character case

In fact S///,s###,[email protected]@@ 都 the same, \ (\), \1,\2

&: The reference pattern matches the entire string.

Practice:

    1. Remove whitespace from the beginning of the/etc/grub.conf file

      Sed-r ' s/^[[:space:]]+//g '/etc/grub.conf

    2. Replace the number in the "Id:3:initdefault:" line in the/etc/inittab file with 5

      Sed-r ' S#^\ (id:\): [[:d igit:]]\ (: initdefault:\) #\15\2#g '

    3. Delete blank lines in the/etc/inittab file

      Sed-r '/^$/d '/etc/inittab

    4. Delete the # number at the beginning of the/etc/inittab file

      Sed-r ' s/^#//g '/etc/inittab

    5. Delete the # and trailing white space characters at the beginning of a file (/etc/inittab), but require that the # sign must have a space character after it

      Sed-r ' s/^#[[:space:]]+//g '/etc/inittab

    6. Remove a directory name for a file path

      Sed-r ' [email protected]^ (/.*/) [^/]+/[email protected]\[email protected] '

    7. Remove the file name of a file path

      Sed-r ' [Email protected]^/.*/([^/]+)/[email Protected]\[email protected] '

sed command in Linux

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.