Regular Introduction_sed

Mission 17 October

9.4/9.5 sed

 

sed tools

Matching printing

  • - n Prints only matching rows, otherwise other rows will be printed.
  • p Printing (with - n)
[root@centos7 tmp]# sed -n '/root/'p passwd 
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
  • I. Ignore case (uppercase i)
# Pay attention to the location of I
[root@centos7 tmp]# sed -n '/castiel/'Ip passwd
castiel:x:1000:1000::/home/castiel:/bin/bash
CASTIEL:X:1000:1000::/HOME/CASTIEL:/BIN/BASH
  • regular
# ^ What to begin with
[root@localhost ~]# sed -n '/^ro/'p text.txt 
root:x:0:0:root:/root:/bin/bash

# Symbols. Replacement of a single character
[root@localhost ~]# sed -nr '/r..t/'p text.txt 
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

Print row

  • Print the specified line
[root@localhost ~]# sed -n '2'p text.txt 
bin:x:1:1:bin:/bin:/sbin/nologin
  • Print a paragraph (several consecutive lines)
[root@localhost ~]# sed -n '2,4'p text.txt 
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
  • All Printing (Head and End Range)
[root@localhost ~]# sed -n '1,$'p text.txt 
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
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:998:996:User for 
...

Content deletion

  • d delete
# Delete 1-5 lines, only to print the content of the screen, the text content actually remains unchanged
# To work on content, add-i
[root@centos7 tmp]# sed -n '1,5'd test.txt
  • - i Synchronize operations to text
Really delete 1-5 lines of text
[root@centos7 tmp]# sed -ni '1,5'd test.txt

Content substitution

  • s / range, regularity, etc. / replaced content / g (without g at the end, replacing only the first matched string in a line)
Specific Character Matching Replacement (when the first and all of the lines are replaced)
# Head Replacement
[root@centos7 tmp]# sed -n 's/cas/CSa/'p test.txt
CSatiel:x:1000:1000::/home/castiel:/bin/bash

# Full replacement
[root@localhost ~]# sed -n 's/cas/CAS/g'p /etc/passwd
CAStiel:x:1000:1000::/home/CAStiel:/bin/bash
  • - r Replaces content in a regular way
# Using Extended Regularity + Matching Content
[root@localhost ~]# sed -nr 's/o+t/1/g'p test.txt 
r1:x:0:0:r1:/r1:/bin/bash
operator:x:11:0:operator:/r1:/sbin/nologin
  • Delete characters in text (essentially replacing letters with empty ones)
[root@localhost ~]# sed -n 's/[a-zA-Z]//g'p test.txt | head
::0:0::/://
::1:1::/://
::2:2::/://
::3:4:://://
::4:7::///://
::5:0::/://
::6:0::/://
::7:0::/://
::8:12::///://
::11:0::/://

Multi-condition matching

Multiple matching conditions (multiple, each condition preceded by - e)

  • - e'/matching condition/'
[root@centos7 tmp]# sed -n -e '/root/'p -e '/castiel/'p text.txt
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
castiel:x:1000:1000::/home/castiel:/bin/bash

sed command supplement

  • - i parameter extension (careful use)
-i[suffix] or --in-place=[suffix]
//Specifies that suffix can create a copy of the text before the operation and save the content at execution time.
[root@centos7 tmp]# head -n 5 /etc/passwd > test.txt
[root@centos7 tmp]# cat test.txt
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

//Execute the operation and save the previous text as a copy
[root@centos7 tmp]# sed -i\.bak '1'd passwd
//or
[root@centos7 tmp]# sed --in-place=.bak '1'd passwd

[root@centos7 tmp]# ll
//Total dosage 8
-rw-r--r--. 1 root root 151 10 Month 311:17 passwd
-rw-r--r--. 1 root root 183 10 Month 311:17 passwd.bak
[root@centos7 tmp]# cat passwd
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
[root@centos7 tmp]# cat passwd.bak 
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
  • Extended regular grouping uses () to surround a group of content, followed by 1 correspondence, and the - r parameter must
Swap the last column with the first
[root@centos7 tmp]# sed -nr 's/([^:]+):(.*):([^:]+)/\3:\2:\1/g'p passwd
/bin/bash:x:0:0:root:/root:root
/sbin/nologin:x:1:1:bin:/bin:bin
/sbin/nologin:x:2:2:daemon:/sbin:daemon
/sbin/nologin:x:3:4:adm:/var/adm:adm
/sbin/nologin:x:4:7:lp:/var/spool/lpd:lp
/bin/sync:x:5:0:sync:/sbin:sync
/sbin/shutdown:x:6:0:shutdown:/sbin:shutdown
/sbin/halt:x:7:0:halt:/sbin:halt
/sbin/nologin:x:8:12:mail:/var/spool/mail:mail
/sbin/nologin:x:11:0:operator:/root:operator
  • & Represents the matching values in previous ()
Each line of matching is prefixed#(Can be used as annotation function)
[root@centos7 tmp]# cat passwd | sed -r 's/(.*)/#&/g'
#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
#shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
#halt:x:7:0:halt:/sbin:/sbin/halt
#mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
#operator:x:11:0:operator:/root:/sbin/nologin

Tags: Operation & Maintenance ftp network DBus

Posted on Fri, 01 Feb 2019 08:54:15 -0800 by yendor