The basic usage of sed and the usage of sed

Source: Internet
Author: User

The basic usage of sed and the usage of sed

Sed basic workflow

Sed: Stream EDitor (by default, the original text is not processed, and only the data in the mode space is processed. After the processing is completed, the content of the mode space is printed to the screen)-row EDitor

Sed reads files to the mode space (memory) row by row and performs mode matching. If the mode is met, edit the file (depending on the editing command) and then perform the next row.

Sed [option] 'addresscommand' file...

-N: silent mode. Content in the mode space is no longer displayed by default.

-I: directly modify the original file

-E SCRIPT: multiple scripts can be executed simultaneously.

-F: execute commands using script files

-R: indicates using an extended regular expression.

Address:

1. StartLine and EndLine

For example, 1,100 (1st rows to 100th rows)

$: Indicates the last row.

2./RegExp (Regular Expression )/

For example,/^ root/

3./pattren1/,/pattren2/

The row first matched by mode 1 starts to the end of the row first matched by Mode 2. All rows in the middle

4. LineNumber:

Specify row

5. StartLine, + N

Start from startline, N rows backward

Command:

D: delete rows that meet the conditions.

P: displays rows that meet the condition (-n)

A \ string: append a new row to the end of the specified row. The content is string.

\ N: used for line feed

I \ string: Add a new line before the specified row with the content of string

R FILE: add the content of the specified FILE to a qualified row.

W FILE: saves the specified range of content to the specified FILE.

S/pattern/string/modifier: search for and replace, replace the content matched by pattern with the string name (by default, only the string that is matched by the pattern for the first time in each line is replaced)

Modifier:

G: Global replacement

I: case insensitive

S //; s ####; s @, also supports back-reference "\ (\), \ 1, \ 2"

&: The reference mode matches the entire string.

sed 's#l..e#&r#g' sed.txtsed 's#\(l..e\)#\1r#g' sed.txt sed 's#l\(..e\)#L\1#g' sed.txtnano sed.txthello, likehi, my love

Sed exercises:

1. delete/etc/grub. the blank line at the beginning of the line in the conf file; 2. Replace "id: 3: initdefault:" in the/etc/inittab file with the number 5 in a row; 3. Delete blank lines in the/etc/inittab file; 4. Delete the # in the/etc/inittab file; 5. Delete the # And the white space characters in the header of an object, but the # must be followed by a white space character; 6. Delete the blank characters starting with the # class line after the blank characters in an object and #7. Retrieve the directory name and base name of a file path respectively.
1、sed -r 's@^[[:space:]]+@@g' /etc/grub.conf2、sed -r 's@(id:)[0-9](:initdefault:)@\15\2@g' /etc/inittab   sed 's@\(id:\)[0-9]\(:initdefault:\)@\15\2@g' /etc/inittab3、sed '/^$/d' /etc/inittab4、sed 's@^#@@g' /etc/inittab5、sed 's@^#[:space:]\{1,\}@@g' /etc/inittab   sed -r 's@^#[[:space:]]+@@g' /etc/inittab6、sed -r 's@^[[:space:]]+#@@g' /etc/inittab   sed 's@^[[:space:]]\{1,\}#@@g' /etc/inittab7、echo "/etc/rc.d" | sed -r 's@^(/.*/)[^/]+/@\1@g'   echo "/etc/rc.d" | sed -r 's@^/.*/([^/])+/@\1@g'

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.