shell--Learning SED

Source: Internet
Author: User

SED: Data Flow Editor

Reads a row into the memory processing row and then outputs a row.

Mode space:

SED: Do not edit source files by default

sed [option] Addresscommand file

1, start line, end line

sed startline,endline file sed 2,5p file

2. Pattern matching/mode/

Sed/^root/p file matches to print two times, because each of itself is printed

3,/mode 1/,/mode 2/

Sed/^l/,/^h/p file begins with a line of L to a line ending with H, meaning the first matching

4, give only a numeric value, line number, indicating the precise designation of a row

Sed 5p file

5, specify the starting line, using +n, to start from the specified line, the backward N rows, a total of n+1 line content
Sed 10,+5 file

SED command:
P: Show rows that match the criteria
D: Delete qualifying line sed/^ '/d file delete line beginning with whitespace
A:append usage: A \string appends a new line after the specified line with the contents of string

# sed '/d$/a \hello world\nhello World ' file adds two lines after ending with D

In fact, the content as a string appended, just \ n in the text is treated as a newline character
I:insert usage: I \string insert a new line before the specified line. Content is string \ n
R:r filename Adds the contents of the specified file to the qualifying line at sed '/d$/r/root/t2 ' t
W:w filename Saves the specified range of content to the specified file (repeated overrides) Sed/d$/w/tmp/d.txt T
S: Find and replace. s/Match mode/content to replace/;@ # $% A
Each line is replaced with all matching items:
G: Global replacement sed s/d/d/g file sed/s/l. e/&r/g file & refers to the whole of the match

I: Ignoring the case of characters when looking for

Options for SED:

-N: Static mode, does not display the contents of the pattern space, only matches the line of the condition sed-n 1p file
-R: Use extended regular expression sed-r s/(L.. e) (m+)/\1r\2xx/g file \1 refers to the whole (back reference) in parentheses without escaping parentheses
-I: Modify source files directly

Practice:
There is a text file:

Username,age,gender,salary
zhangsan,20,f,2000

A back reference to SED:
& represents the entire string for a reference pattern match


Practice:
1. Delete the blank character from the beginning of the result of the history command [[: Space:]]

History | Sed s/^[[:space:]]//g
2. Take out the parent directory of a file path such as/var/log/mysql/acces.log ===>/var/log/mysql/
echo "/var/log/mysql" | Sed-r ' s#^ (/?. */). +/?#\1#g '
3. Delete files with white space characters + #开头的行中的空白字符及 #
#asdsdasd
==>
Asdasd
Sed-r s/^[[:space:]]+#//g File

Sed-r ' s/^ (l.*e$)/#\1/g ' file add comment to L ending with E

shell--Learning SED

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.