Linux commands: Introduction to SED

Source: Internet
Author: User
Tags line editor

Sed is an online line editor that processes one line at a time. When you are working, place the currently processed rows in the "mode space" for editing, and then transfer the content to the screen after editing is complete.

Syntax: sed [OPTION] ... {Script} ... [Input file]

Options:

-R: Supports regular expressions

-N: Silent mode, content not displayed

-E:SCRIPT1-E script2-e SCRIPT3: Specify multi-script run

-f/path/to/script_file: Reads the script from the specified file and runs

-I: Modify source files directly


Command:

D: Delete rows in the pattern space;

=: Display line number;

A \text: Append text

I \text: Insert text, support \ n implement multi-line insertion;

C \text: Replace the matching line with text;

P: Print the lines in the mode space;

s/regexp/replacement/: Replace the content that is matched by regexp to replacement;

G: global substitution;

W/path/to/somefile: Save the specified content to the file specified by the/path/to/somefile path;

R/path/from/somefile: Inserts all the contents of another file at the specified location in the file, completing the file merge


Regular:

Character matching:., [], [^]

Number of matches: *, \?, \+, \{m,n\}, \{n\}

Position anchoring: ^, $, \<, \>

Grouping and referencing: \ (\), \1, \2, ...

Multiple Choice one: a|b|c


Delimitation:

#: Specify line;

$: last line;

/regexp/: Any row that can be matched to by regexp;

\%regexp%: Ibid., except for the regexp boundary symbol;

/regexp/| :

\%regexp%| : Ignoring character case when matching;

Startline,endline:

#,/regexp/: Starting from # line, to the end of the line to which the first/regexp/is matched, all rows in the middle;

#,#

/regexp1/,/regexp2/: Starting from the first line that is matched to the/regexp1/, to the end of the line that is first matched to the/regexp2/, all rows in the middle;

#,+n: Starting with # lines, up to n rows down;

First~step: Specify the starting line, and the step size;

1~2,2~2


Advanced command:

H: Use the content of the pattern space to cover the content of keeping the space;

H: Append the contents of the pattern space to the content in the holding space;

G: Take its content from the hold space and overwrite it with the contents of the pattern space;

G: Take the content from the hold space and append it to the content in the pattern space;

X: Exchanging in the space of keeping and pattern;

N: Reads the next line from the matching row to the pattern space (overwrites the original content in the pattern space);

N: Reads the next line to the pattern space of the matched row, appended to the original content in the pattern space;

D: Delete the contents of the pattern space;

D: Delete the first row in multi-line mode space;

Note: Command function can be used! Reverse; semicolons can be used to separate scripts;


Practice:

1. Delete whitespace characters at the beginning of all lines in the/root/file file

[email protected] ~]# cat file && cat file |wc-l

Aaaaaaaaaaaaaaaaa

bbbbbbbbbbbbbbbb

Cccccccccccccccc


2. Remove all # and white-space characters from the beginning of the line in the/etc/fstab file that begin with #, followed by at least one white-space character

[[Email protected] ~]# sed ' s/^#[[:space:]]\+//'/etc/fstab

#

/etc/fstab

Created by Anaconda on Wed 26 23:57:24 2015

#

Accessible filesystems, by reference, is maintained under '/dev/disk '

See Mans Pages Fstab (5), Findfs (8), mount (8) and/or Blkid (8) for more info

#

/DEV/MAPPER/VOLGROUP-LV_ROOT/EXT4 Defaults 1 1

Uuid=ccfe9f33-b5da-48b1-821b-b3bec206147b/boot EXT4 Defaults 1 2

/dev/mapper/volgroup-lv_swap swap swap defaults 0 0

TMPFS/DEV/SHM TMPFS Defaults 0 0

Devpts/dev/pts devpts gid=5,mode=620 0 0

Sysfs/sys Sysfs Defaults 0 0

PROC/PROC proc Defaults 0 0


3. View only lines 1th through 5th of the/etc/fstab file

[Email protected] ~]# sed-n ' 1,5p '/etc/fstab

#

#/etc/fstab

# Created by Anaconda on Wed 26 23:57:24 2015

#


4. Delete rows from the file that contain "my" to rows that contain "you"

[[email protected] ~]# cat file

This is my test line

How is You

Hello

How is You tom

My

Aaaaaaaaaaaaa

You

[[Email protected] ~]# sed '/my/,/you/d ' file

Hello

How is You tom


5. Query all rows that contain "you"

[[email protected] ~]# sed-n '/you/p ' file

How is You

How is You tom

You


6. Add a blank line after each line in the file

[[Email protected] ~]# sed ' G ' file

This is my test line

How is You

Hello

How is You tom

My

Aaaaaaaaaaaaa

You


7. Ensure that the specified file has only one blank line behind each line

[[Email protected] ~]# sed '/^$/d; G ' File

This is my test line

How is You

Hello

How is You tom

My

Aaaaaaaaaaaaa

You


8. Print odd lines

[[email protected] ~]# sed-n ' 1~2p ' file

1,this is my test line

3,hello

5,my

7,you

Linux commands: Introduction to SED

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.