Linux Regular expression sed

Source: Internet
Author: User

SED, awk tool enables text substitution and outputs the replaced text to the screen

Both SED and awk are streaming editors that operate on the lines of the document. SED is often used to replace operations.


The text content of the sample, the following actions are based on this text.

[email protected] ~]# cat Test.txt Rot:x:0:0:rot:/rot:/bin/bashroot:x:0:0:root:/root:/bin/bashdaemon:x:2:2:daemon :/sbin:/sbin/nologinrooooot:x:0:0/roooooot:/bin/ Bash11111111111111111111111111111111aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa


1. Print a line

sed -n ‘n‘p filenameThe N in single quotation marks is a number representing the first line, and P can also be written in quotation marks;

example, print passwd 第3-5 line;

[Email protected] ~]# sed-n ' 3,5 ' p/etc/passwddaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/ Nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

Print all Lines

[Email protected] ~]# sed-n ' 1,$ ' p/etc/passwd

Print a line and display the line number, which needs to be used with grep;

[[email protected] ~]# grep-n '. * ' Test.txt | Sed-n ' 1 ' P1:RRT


2. Print a line containing a string

The string needs to be enclosed in// , and the special symbol in grep is ^ $. * The same applies to SED.

[Email protected] ~]# sed-n '/root/' p/etc/passwdroot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/ Sbin/nologin


Print lines containing/sbin/nologin,/need to be de-intended;

[Email protected] ~]# sed-n '/\/sbin\/nologin/' P passwd bin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/ Sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

Print lines that begin with D, print with the end of an in, 2 arbitrary characters in the middle of the print R and O letters, and print lines containing 2 o and more O;

[[email protected] ~]# sed-n '/^d/' P test.txt daemon:x:2:2:daemon:/sbin:/sbin/nologin[[email protected] ~]# sed-n '/in$/ ' P test.txt daemon:x:2:2:daemon:/sbin:/sbin/nologin[[email protected] ~]# sed-n '/R. o/' P test.txtrooooot:x:0:0/roooooot:/bin/bash[[email protected] ~]# sed-n '/ooo*/' P test.txtroot:x:0:0:root:/root:/ Bin/bashrooooot:x:0:0/roooooot:/bin/bash


3.-E can achieve multiple behaviors

[[email protected] ~]# sed-e ' 1 ' p-e '/111/' p-n test.txt rot:x:0:0:rot:/rot:/bin/bash11111111111111111111111111111111

Can write multiple with-E, and-e equivalent to the belt, first match the front of the printing, and then match the subsequent printing, so the first line printing 2 times;

[[email protected] ~]# sed-e '/root/p '-e '/bash/p '-n 1.txtroot:x:0:0:root:/root:/bin/bashroot:x:0:0:root:/root:/bin/ bashoperator:x:11:0:operator:/root:/sbin/nologinuser1:x:500:500::/home/user1:/bin/bashuser2:x:501:501::/home/ User2:/bin/bash


-E can also be used after a semicolon;-n cannot be written to the back of-e, and can be written in front of or at the back of-e;

[[email protected] ~]# sed-e '/root/p;/bash/p '-n 1.txtroot:x:0:0:root:/root:/bin/bashroot:x:0:0:root:/root:/bin/ bashoperator:x:11:0:operator:/root:/sbin/nologinuser1:x:500:500::/home/user1:/bin/bashuser2:x:501:501::/home/ User2:/bin/bash


4. Delete a row or multiple lines

< Span style= "Font-family:simsun;font-size:14px;color:rgb (0,0,0); background-color:inherit;" > ' d '  


Example, delete the first row, delete 1-3 rows, delete the row containing the root keyword, delete the 3rd line to the last row of all rows;

[[Email protected] ~]# sed ' 1 ' d test.txt root:x:0:0:root:/root:/bin/bashdaemon:x:2:2:daemon:/sbin:/sbin/ Nologinrooooot:x:0:0/roooooot:/bin/bash11111111111111111111111111111111aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[[email Protected] ~]# sed ' 1,3 ' d test.txt rooooot:x:0:0/roooooot:/bin/ Bash11111111111111111111111111111111aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[[email protected] ~]# sed '/root/' d test.txtrot:x:0:0:rot:/rot:/bin/bashdaemon:x:2:2:daemon:/sbin:/sbin/nologinrooooot:x:0:0/roooooot:/bin/ Bash11111111111111111111111111111111aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[[email protected] ~]# sed ' 3,$ ' d test.txt rot:x : 0:0:rot:/rot:/bin/bashroot:x:0:0:root:/root:/bin/bash


5. Replace character or replace string

example, replace the first line of the letter R to R; Replace the OT in line 1-2 to; Replace all rows with OT as to;

[[Email protected] ~]# sed ' 1s/r/r/g ' test.txt rrt[[email protected] ~]# sed ' 1,2s/ot/to/g ' test.txt rto:x:0:0:rto:/rto:/b in/bashroto:x:0:0:roto:/roto:/bin/bashdaemon:x:2:2:daemon:/sbin:/sbin/nologinrooooot:x:0:0/roooooot:/bin/ Bash11111111111111111111111111111111aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[[email protected] ~]# sed ' s#ot#to#g ' test.txt rto:x:0:0:rto:/rto:/bin/bashroto:x:0:0:roto:/roto:/bin/bashdaemon:x:2:2:daemon:/sbin:/sbin/nologinrooooto:x:0 : 0/ROOOOOTO:/BIN/BASH11111111111111111111111111111111AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

Replace the same command format with the Vim editor, S for replace, G for Global, and no G to replace only the first of the lines; You can use # @ as the delimiter.

the 1s represents only the first line; 1,3s represents the first row to the third row; The default is all rows if no line count is added.


example, remove all the numbers in the document ' s/[0-9]//g ' as deleting all the letters in the document ' s/[a-za-z]//g ' to remove all the numbers and letters ' s/[0-9a-za-z]//g '

[[Email protected] ~]# sed ' s/[0-9]//g ' test.txt rot:x:::rot:/rot:/bin/bashroot:x:::root:/root:/bin/bashdaemon:x::: Daemon:/sbin:/sbin/nologinrooooot:x::/roooooot:/bin/bashaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

example, delete all the letters in the document

[[Email protected] ~]# sed ' s/[a-z]//g ' 1.txt:: 0:0:/://::11:0::/://::11:0::/://::0:0:/:// 1111111111111111111111111111111

Example to delete a document that is not in the English alphabet (numbers and special symbols)

[[Email protected] ~]# sed ' s/[^a-z]//g ' 1.txt Rotxrotbinbashoperatorxoperatorrootsbinnologinoperatorxoperatorroootsbinnologinrooootxroooootbinbashaaaaaaaaaaaaaaaaaaaaa Aaaaaaaaaa

example, deleting a document that is not a number (English letters and special symbols)

[[Email protected] ~]# sed ' s/[^0-9]//g ' 1.txt 00110110001111111111111111111111111111111


example, replace all lowercase letters in the document with the uppercase letters ' s/[a-z]/\u&/g '

[[Email protected] ~]# sed ' s/[a-z]/\u&/g ' 1.txt rot:x:0:0:/rot:/bin/bashoperator:x:11:0:operator:/root:/sbin/ nologinoperator:x:11:0:operator:/rooot:/sbin/nologinroooot:x:0:0:/rooooot:/bin/ Bash1111111111111111111111111111111aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

example, replace all uppercase letters in the document with the lowercase ' s/[a-z]/\l&/g '

[[Email protected] ~]# sed ' s/[a-z]/\l&/g ' 1.txt rot:x:0:0:/rot:/bin/bashoperator:x:11:0:operator:/root:/sbin/ nologinoperator:x:11:0:operator:/rooot:/sbin/nologinroooot:x:0:0:/rooooot:/bin/ Bash1111111111111111111111111111111aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Replace with-n p to print a specific line;

[[email protected] ~]# sed-n ' 1s/[a-z]/\u&/g ' P 1.txt rot:x:0:0:/rot:/bin/bash


6, swap the position of two strings

Sed-r ' s/([^:]+) (:. *:) ([^:]+$)/\3\2\1/'

[^:] non-colon, not all characters of the colon

-R,--regexp-extended extended Regular expressions are used in scripts, and the plus r option means There is no need to add any more meaning in the back

() brackets are surrounded by the meaning of fragmentation,

\3\2\1 is representing 3 swap 1, after which the content is swapped to the front


example, AA and FF Exchange, plus-r option without the caret \; no-r () brackets around the need to add \ de-meaning; swap multiple lines with the last one;

[email protected] ~]# cat 1.txt aa:bb:cc:dd:ee:ff[[email protected] ~]# sed-r ' s/(AA) (. *) (FF)/\3\2\1/' 1.TXTFF:BB:CC:DD : Ee:aa[[email protected] ~]# sed ' s/\ (aa\) \ (. *\) \ (ff\)/\3\2\1/' 1.txt ff:bb:cc:dd:ee:aa[[email protected] ~]# sed-r ' s/( [^:]+] (:. *:) ([^:]+$)/\3\2\1/' 1.txt Ff:bb:cc:dd:ee:aa


example, the rot is swapped with the/bin/bash

[[email protected] ~]# grep ' rot ' test.txtrot:x:0:0:rot:/rot:/bin/bash[[email protected] ~]# grep ' rot ' test.txt |sed ' s/\ (rot\) \ (. *\) \ (\/bin\/bash\)/\3\2\1/'/bin/bash:x:0:0:rot:/rot:rot[[email protected] ~]# grep ' rot ' test.txt |sed-r ' s/ (ROT) (.*) (/bin/bash)/\3\2\1/' sed:-e expression #1, characters: "s" unknown option [[email protected] ~]# grep ' rot ' test.txt |sed-r ' s/(Rot) (. *) (\/bin\/ba SH)/\3\2\1/'/bin/bash:x:0:0:rot:/rot:rot

Add-R after ()/bin/bash also need to take off the idea to be able;


The-I option replaces characters directly and saves the original file. Remember to back up the original file before using the I option.

[[email protected] ~]# sed-r-i ' s/([^:]+] (:. *:) ([^:]+$)/\3\2\1/' 1.txt [[email protected] ~]# cat 1.txt ff:bb:cc:dd:ee:a A




This article is from the "Model Student's Learning blog" blog, please be sure to keep this source http://8802265.blog.51cto.com/8792265/1632293

Linux Regular expression 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.