Sed command details and examples

Source: Internet
Author: User
Tags line editor

To be flexible and process text as needed, we need to fully learn and master three major text processing tools: grep, sed (data stream Editor), and awk (Report Text generator)

The following describes and describes the basic usage of the SED command.

Sed, also known as stream editer, is a line editor (which includes a word processor and a text editor). It is a text editor that operates plain ASCII code text, and Operation text is operated by line, text is processed line by line. By default, text itself is not directly processed. During the operation, the text is read to the memory line by line, after processing in the memory, the row matching the mode is displayed on the screen. The memory space is called the SED mode space. The default value is the print mode space.

Synopsis

Sed [Option]... {script-only-if-no-other-script} [input-file]...

Sed [Option]... 'address' file... address and command

$: Indicates the last line in the text.

Option:

-N: Indicates silent mode. The content in the mode space is not displayed. It is only related to commands.

-I: directly modify the original file

-E script: multiple scripts can be executed simultaneously.

-F/path/to/sed_script: indicates that the script in the file is read to process the text.

Sed-F/script file ..

-R: indicates using an extended regular expression.

Address:

1. Startline and endline, for example, 1 and 10 indicate rows from the first line to 10th;

2./pattern (Regexp Regular Expression)/, for example,/^ root/. Find the line starting with root.

3./pattern1/,/pattern2/indicates all rows in the middle of the row that is matched by mode 1 for the first time and that is matched by Mode 2 for the first time.

4. linenumber indicates a batch row or a specific row.

5. Startline, + N, indicating the N rows after Startline, n + 1 rows in total;

Command:

D: Delete the row that meets the condition;

For example, delete the first four rows in the/etc/fstab file. From the first row to the second row, the values are 1, $-1.

                [[email protected] ~]# sed "1,4d" /etc/fstab

For example, delete a row containing sys.

                [[email protected] ~]# sed "/sys/d" /etc/fstab

For example, delete the content of the first row and the last 10 rows.

                [[email protected] ~]# sed "1,+10d" /etc/fstab

For example, to delete only the fifth line

                [[email protected] ~]# sed "5d" /etc/fstab

P: displays the rows that meet the conditions. By default, the display mode space matches the command once, twice in total.

For example, display the lines starting with/in the file and only display the rows that meet the condition.

                [[email protected] ~]# sed -n "/^\//p" /etc/fstab

A \ Sting: adds a new row after the batch or matched row with the content of string

For example, add a new line after the line starting with "# It is a blank line ."

                [[email protected] ~]# sed ‘/^\//a \# It is a blank line.‘ /etc/fstab

For example, add two lines after the line starting with "/" with the content "# It is first blank line." "# It is second blank line ."

                 [[email protected] ~]# sed ‘/^\//a \# It is first blank line.\n# It is second  blank line‘ /etc/fstab

I \ Sting: adds a new row before the batch or matched row with the content of string

R filename: adds the specified file content to the specified row to merge the file content.

For example, after adding the content of the/etc/issue file to the second line of the/etc/fstab file

                [[email protected] ~]# sed ‘2r /etc/issue‘ /etc/fstab

For example, add the content of the/etc/issue file to the first and second lines of the/etc/fstab file

                [[email protected] ~]# sed ‘1,2r /etc/issue‘ /etc/fstab

W filename: saves content within a specified range to a specified file.

For example, you can use the-n option to save the file content that contains oot to the/tmp/oot. tmp style.

                [[email protected] ~]# sed ‘/oot/w /tmp/oot.tmp‘ /etc/fstab

S/pattern (metacharacters of regular expressions can be used)/string/modifier: searches for matched pattern strings and replaces them with string, the default value is the first pattern-matched string in each row. If you want to replace all the pattern-matched strings in the row, you need to add a modifier; the/can be replaced with other characters to ensure consistency without escaping, such as ###,, and %.

G: Global replacement Modifier

I: Case Insensitive when searching

For example, replace the/etc/fstab string containing the lower-case oot with the upper-case oot

                   [[email protected] ~]# sed ‘s/oot/OOT/‘ /etc/fstab

For example, replace/in the/etc/fstab line starting with/#

                   [[email protected] ~]# sed ‘s/^\//#/‘ /etc/fstab

For example, replace all/in/etc/fstab #

                   [[email protected] ~]# sed ‘s/\//#/g‘ /etc/fstab

Backward reference:

\ (\): \ 1, \ 2, etc.

&: Indicates the entire string matched by the pattern.

For example, if we find that e starts with L and ends with any two characters in the middle, replace it with R

The file content is as follows:

                    [[email protected] ~]# cat sed.txt                      he like.                     her love.

#1) the execution result is as follows:

                    [[email protected] ~]# sed ‘s%l..e%&r%‘ sed.txt                     he liker.                    her lover.                    #2)                                        [[email protected] ~]# sed ‘s%\(l..e\)%\1r%‘ sed.txt                         he liker.                    her lover.

To match a part of a search string, you can only use backward reference, but not &

For example, when l. E, like --> like, love --> love, only backward reference can be used.

The execution result is as follows:

                    [[email protected] ~]# sed ‘s%l\(..e\)%L\1%‘ sed.txt                           he Like.                    her Love.

For example, delete the blank first line of the execution result in the History command and retrieve the row number.

                [[email protected] ~]# history | sed -r ‘s#^[[:space:]]+##g‘ |cut -d‘ ‘ -f1

Exercise questions:

1. Delete the blank characters at the beginning of the/etc/grub. conf file

        [[email protected] ~]# sed  -r ‘s#^[[:space:]]+##g‘ /etc/grub.conf

2. Replace ID: 3: initdefault: 3 in the/etc/inittab file with 6

        [[email protected] ~]# sed ‘s#\(id:\)[0-9]\(:initdefault:\)#\16\2#g‘ /etc/inittab

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

        [[email protected] ~]# sed ‘/^$/d‘ /etc/inittab

4. Delete the # header in the/etc/inittab file line

        [[email protected] ~]# sed ‘s/^#//g‘ /etc/inittab

5. Delete the first line of a file # And Its trailing blank characters

The file content is as follows:

        [[email protected] ~]# cat b.txt         #    It is a blank.             #It is second blank.

Execution result:

        [[email protected] ~]# sed -r ‘s%^#[[:space:]]+%%g‘ b.txt         It is a blank.             #It is second blank.

6. Delete the blank characters at the beginning of a file and Its trailing #

The file content is as follows:

        [[email protected] ~]# sed -r ‘s%^[[:space:]]+#%%g‘ b.txt          #    It is a blank.        It is second blank.

7. Retrieve the parent directory of a path

        [[email protected] ~]# echo "/etc/rc.d/rc4.d/S99local" | sed -r ‘[email protected](/.*/)[^/]+/[email protected]\[email protected]‘/etc/rc.d/rc4.d/


This article from the "ignorance and knowledge" blog, please be sure to keep this source http://yujiqing.blog.51cto.com/1475974/1616458

Sed command details and examples

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.