Self-study Linux instruction analysis-sed

Source: Internet
Author: User

Self-study Linux instruction analysis-sed

1 · command format

sed [-NEFRI]/Input text/' command ' file

2 · Command parameters

-N cancels the default output

-I changes the content of the output

-E makes multiple edits, which are used when multiple SED commands are applied to the input rows. Action editing of SED directly in command-column mode

-f Specifies the file name of the SED script. Directly write the SED action in a file, and-f filename to perform the SED action within the filename

The action of the-R SED supports the syntax of the extended regular expression. (presets are the underlying regular expression syntax

Common directives:
A: New, a can be followed by a string, and these strings will appear on a new line (the next line)
C: Replace, C can be followed by a string, these strings can replace the line between N1,N2
D: Delete, because it is deleted, so D usually does not pick up any content after
I: Insert, I can be followed by a string, and these strings will appear on a new line (the current line)
P: Printing, that is, printing out a selected material. Usually P is used in conjunction with the parameter sed-n
S: Replace, can be directly to replace the work. Usually this s action can be paired with regular expressions. such as 1,20s/old/new/g

3. Command function

(Delete, modify, replace, add) One of the core commands, (Taoyuan three jieyi, Dick)

4. Command Fan List

Real one: The known file Test.txt content is:

Test

Liyao

Oldboy

Please give a command that does not contain a Oldboy string when printing test.txt content

Command one: sed/oldboy/d test.txt

[email protected] tmp]# cat Test.txt
Test
Liyao
Oldboy
[Email protected] tmp]# sed/oldboy/d test.txt
Test
Liyao

Command two: Sed-n/oldboy/p test.txt only show Oldboy

[email protected] tmp]# cat Test.txt
Test
Liyao
Oldboy

[Email protected] tmp]# sed-n/oldboy/p test.txt
Oldboy

[Email protected] ~]# sed-n 20,30p ett.txt

20

。。。

30

Replace all strings that contain oldboylinux in files that end with extension ett.txt in the/directory and their subdirectories with Dboywindow

[Email protected] ~]# echo oldboylinux >ett.txt

[email protected] ~]# cat Ett.txt

    • Oldboylinux

[email protected] ~]# CP ett.txt/etc/

[email protected] ~]# CP ett.txt/opt/

[[Email protected] ~]# sed s#oldboylinux#oldboywindows#g ett.txt This bar just changed the output, but the source file has not changed

(s = edit Replace

g = Replace All)

    • Oldboywindows

[email protected] ~]# cat Ett.txt

Oldboylinux

[[email protected] ~]# sed-i s#oldboylinux#oldboywindows#g ett.txt (sed–i means changing content)

[email protected] ~]# cat Ett.txt

    • Oldboywindows

[[email protected] ~]# Find/-type f-name "Ett.txt" Find the original file

/etc/ett.txt

/root/ett.txt

/opt/ett.txt

[[email protected] ~]# Find/-type f-name "Ett.txt" |xargs sed-i s#oldboylinux#oldboywindows#g Answer

[Email protected] ~]# Cat/etc/ett.txt

    • Oldboywindows

[Email protected] ~]# Cat/opt/ett.txt

Oldboywindows

[[email protected] ~]# Find/-type f-name "Ett.txt" |xargs cat via Pipe | Progressive processing, instruction cat (view content)

    • Oldboywindows
    • Oldboywindows
    • Oldboywindows

[[email protected] ~]# Find/-type f-name "Ett.txt" |xargs ls via pipe | Progressive processing, command LS (view list)

/etc/ett.txt/opt/ett.txt/root/ett.txt

Self-study Linux instruction analysis-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.