Introduction to the Linux centos7 sed tool

Source: Internet
Author: User

First, the SED

The Grep tool feature can only be found and cannot replace the found content.

SED itself is a pipeline command, mainly in the Behavior unit processing, can be the data row to find, delete, replace characters or strings, swap string location , directly modify the contents of the file and other specific work, for the document's lines, such as. txt.

[Email protected] ~]# mkdir SED
[[Email protected] ~]# CD SED
[email protected] sed]# CP. /GREP/PASSWD 0.txt
[Email protected] sed]#
[Email protected] sed]# pwd
/root/sed
[[email protected] sed]# ls
0.txt
[Email protected] sed]#

find:sed-n '/keywords/' p filename

[[email protected] sed]# sed-n ' 2 ' P 0.txt show second line

[[Email protected] sed]# sed ' 15,$ ' p 0.txt display 15 lines to the last line

[[email protected] sed]# sed-n ' 1,$ ' P 0.txt Show all

[[email protected] sed] #sed-n '/^1/' P 0.txt

[[email protected] sed] #sed-n '/in$/' P 0.txt

[[email protected] sed] #sed-n '/R. o/' P 0.txt

[[email protected] sed] #sed-n '/oo*/' P 0.txt

[[email protected] sed] #sed-n '/oo*/' Ip 0.txt Add uppercase I, case-insensitive

Delete:sed-n ' keyword ' p file name, simply list the rows not deleted in the screen, in fact the original file is not deleted.

[[email protected] sed] #sed-n ' 5 ' p 0.txt

[[email protected] sed] #sed-n ' 1,5 ' p 0.txt

[[email protected] sed] #sed-n ' 1,$ ' P 0.txt

[[email protected] sed] #sed-n '/root/' P 0.txt

[[email protected] sed] #sed-n '/^1/' P 0.txt

[[email protected] sed] #sed-n '/in$/' P 0.txt

[[email protected] sed] #sed-n '/R. o/' P 0.txt

[[email protected] sed] #sed-n '/oo*/' P 0.txt

Replace: s denotes substitution action, G denotes the bank's global substitution, except with/As a delimiter, can also use special characters #, @

[[Email protected] sed]# sed ' 1,2s/ot/to/g ' 0.txt 12th line, OT replaced with to

[Email protected] sed]# sed-r ' 1,2s/ot+/to/g ' 0.txt

[[Email protected] sed]# sed ' s#ot#to#g ' 0.txt

[Email protected] sed]# sed ' [email protected]@[email protected] ' 0.txt

[[Email protected] sed]# sed ' s/[0-9]//g ' 0.txt

[[Email protected] sed]# sed ' s/[a-za-z]//g ' 0.txt

Swap two character positions

[[Email protected] sed]# sed ' s/\ (root\) \ (. *\) \ (bash\)/\3\2\1/' 0.txt

[[email protected] sed]# Sed-r ' s/(Root) (. *) (bash)/\3\2\1/' 0.txt join-R expression clearer

Bash:x:0:0:root:/root:/bin/root

[Email protected] sed]# sed-r ' s/(Uaer) (. *) (bash)/\3\2\1/' 0.txt

Bash1:x:1001:1004::/home/uaer1:/bin/uaer

[[Email protected] sed]# sed ' s/^.*$/123&/' 0.txt front plus 123

123root:x:0:0:root:/root:/bin/bash
123bin:x:1:1:bin:/bin:/sbin/nologin

Modify file Contents directly

[Email protected] sed]# sed-i ' s/root/toor/g ' 0.txt
[email protected] sed]# cat 0.txt
Toor:x:0:0:toor:/toor:/bin/bash

Example

[[email protected] sed]# sed '/root/' P 0.txt will show all rows
Root:x:0:0:root:/root:/bin/bash
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin:/bin:/sbin/nologin
Daemon:x:2:2:daemon:/sbin:/sbin/nologin
Adm:x:3:4:adm:/var/adm:/sbin/nologin
Lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
Sync:x:5:0:sync:/sbin:/bin/sync
[[email protected] sed]# sed-n '/root/' P 0.txt just show the keyword target line
Root:x:0:0:root:/root:/bin/bash
Operator:x:11:0:operator:/root:/sbin/nologin

[[email protected] sed]# sed-nr '/root|bus/' P 0.txt join R

Root:x:0:0:root:/root:/bin/bash
Operator:x:11:0:operator:/root:/sbin/nologin

[Email protected] sed]# sed-nr '/o+t/' P 0.txt
Root:x:0:0:root:/root:/bin/bash
Operator:x:11:0:operator:/root:/sbin/nologin

Dbus:x:81:81:system message Bus:/:/sbin/nologin

[Email protected] sed]# sed-nr '/o{2}/' P 0.txt
Root:x:0:0:root:/root:/bin/bash
Lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
Mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
Operator:x:11:0:operator:/root:/sbin/nologin
Postfix:x:89:89::/var/spool/postfix:/sbin/nologin

Introduction to the Linux centos7 sed tool

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.