Text processing tool in linux------sed

Basic syntax
sed [OPTION]... {script-only-if-no-other-script} [input-file]...
Automatically print by default

parameter effect
-n Do not output the contents of the mode space to the screen, that is, do not automatically print
[root@centos8 ~ ]#sed '' /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Mon Sep 21 21:32:25 2020
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
UUID=36500084-b1ee-4e0f-a6e5-a970a5338796 /                       xfs     defaults        0 0
UUID=ef517225-47b5-4f1d-b807-0a43d39f77b0 /boot                   ext4    defaults        1 2
UUID=ae2e39f6-476d-406e-bbae-7806cfc52d05 /data                   xfs     defaults        0 0
UUID=eac19fbe-3f3c-4f1f-804e-09458838db1c swap                    swap    defaults        0 0

[root@centos8 ~ ]#sed 'p' /etc/fstab 


#
#
# /etc/fstab
# /etc/fstab
# Created by anaconda on Mon Sep 21 21:32:25 2020
# Created by anaconda on Mon Sep 21 21:32:25 2020
#
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
# units generated from this file.
#
#
UUID=36500084-b1ee-4e0f-a6e5-a970a5338796 /                       xfs     defaults        0 0
UUID=36500084-b1ee-4e0f-a6e5-a970a5338796 /                       xfs     defaults        0 0
UUID=ef517225-47b5-4f1d-b807-0a43d39f77b0 /boot                   ext4    defaults        1 2
UUID=ef517225-47b5-4f1d-b807-0a43d39f77b0 /boot                   ext4    defaults        1 2
UUID=ae2e39f6-476d-406e-bbae-7806cfc52d05 /data                   xfs     defaults        0 0
UUID=ae2e39f6-476d-406e-bbae-7806cfc52d05 /data                   xfs     defaults        0 0
UUID=eac19fbe-3f3c-4f1f-804e-09458838db1c swap                    swap    defaults        0 0
UUID=eac19fbe-3f3c-4f1f-804e-09458838db1c swap                    swap    defaults        0 0
[root@centos8 ~ ]#sed -n 'p' /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Mon Sep 21 21:32:25 2020
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
UUID=36500084-b1ee-4e0f-a6e5-a970a5338796 /                       xfs     defaults        0 0
UUID=ef517225-47b5-4f1d-b807-0a43d39f77b0 /boot                   ext4    defaults        1 2
UUID=ae2e39f6-476d-406e-bbae-7806cfc52d05 /data                   xfs     defaults        0 0
UUID=eac19fbe-3f3c-4f1f-804e-09458838db1c swap                    swap    defaults        0 0
parameter effect
-e script command This option will add the following script commands to the existing commands.
-f script command file This option will add script commands in subsequent files to existing commands.
-r、-E Use regular expressions
-i This option will directly modify the source file and should be used with caution. Use with -i.bak to modify and backup source files

sed script format'address
command'

Address format effect
’ ’ No address, process the full text
‘#p’ Print the content of line #
‘#,#p’ Print everything from #line to #line
‘#,+#p’ Print everything from # line to +# line
‘#,$p’ Print everything from line # to the end of the line
‘/part/p’ Print all lines that contain /match content/
‘/part1/,/part2/p’ Print all lines from /part1/ to /part2/, how many times are matched and printed several times
‘#,/part/p’ Print all lines from line # to the first /part/
1~2 Print odd lines
2~2 Print even lines

Demo: top to bottom

[root@centos8 ~ ]#sed ' ' /etc/issue
\S
Kernel \r on an \m

[root@centos8 ~ ]#seq 10 | sed -n '1p'
1
[root@centos8 ~ ]#seq 10 | sed -n '2,4p'
2
3
4
[root@centos8 ~ ]#seq 10 | sed -n '2,+4p'
2
3
4
5
6
[root@centos8 ~ ]#seq 10 | sed -n '2,$p'
2
3
4
5
6
7
8
9
10

[root@centos8 ~ ]#sed -n '/home/p' /etc/passwd
guan:x:1000:1000:guan:/home/guan:/bin/bash
test:x:1001:1001::/home/test:/bin/bash
gentoo:x:1002:1003:Gentoo Distribution:/home/gentoo:/bin/csh
nginx:x:1003:1005::/home/nginx:/bin/bash
varnish:x:1004:1006::/home/varnish:/bin/bash
tomcat:x:1006:1008::/home/tomcat:/bin/bash
git:x:1007:1010::/home/git:/bin/bash
test2:x:1008:1011::/home/test2:/bin/bash
mageia:x:1100:1100::/home/linux:/bin/bash
slackware:x:2002:2019::/home/slackware:/bin/tcsh
wang:x:2003:2003::/home/wang:/bin/bash
li:x:2004:2004::/home/li:/bin/bash
[root@centos8 ~ ]#sed -n '2,/home/p' /etc/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
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:65534:65534:Kernel Overflow User:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
unbound:x:997:995:Unbound DNS resolver:/etc/unbound:/sbin/nologin
sssd:x:996:993:User for sssd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
rngd:x:995:992:Random Number Generator Daemon:/var/lib/rngd:/sbin/nologin
guan:x:1000:1000:guan:/home/guan:/bin/bash
[root@centos8 ~ ]#sed -n '/games/,/home/p' /etc/passwd
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
unbound:x:997:995:Unbound DNS resolver:/etc/unbound:/sbin/nologin
sssd:x:996:993:User for sssd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
rngd:x:995:992:Random Number Generator Daemon:/var/lib/rngd:/sbin/nologin
guan:x:1000:1000:guan:/home/guan:/bin/bash

[root@centos8 ~ ]#seq 10 | sed -n '2~2p'
2
4
6
8
10
[root@centos8 ~ ]#seq 10 | sed -n '1~2p'
1
3
5
7
9
command effect
p Print the contents of the current mode space
Ip Ignore case output
d Delete rows matching the pattern space
a Append text after the specified line. \nRealize multi-line append
i Insert text before the specified line
! Inversion of matching rows in pattern space
s/pattern/pattern/ (g global, p shows the successful line, w saves, i ignores case) Find and replace, same as vim
[root@centos8 ~ ]#sed -n '/KERNEL/Ip' /etc/issue
Kernel \r on an \m
[root@centos8 ~ ]#sed -n '/Ker/Ip' /etc/issue
Kernel \r on an \m
[root@centos8 ~ ]#sed -n '/ker/Ip' /etc/issue
Kernel \r on an \m
[root@centos8 ~ ]#man sed
[root@centos8 ~ ]#sed -n '/ker/p' /etc/issue
[root@centos8 ~ ]#sed -n '/kernel/p' /etc/issue
[root@centos8 ~ ]#sed -n '/Kernel/p' /etc/issue
Kernel \r on an \m

[root@centos8 ~ ]#seq 10 | sed  '2d' 
1
3
4
5
6
7
8
9
10
[root@centos8 ~ ]#sed  '2d' num.txt 
1
3
4
5
6
7
8
9
10
[root@centos8 ~ ]#sed  '2~2d' num.txt 
1
3
5
7
9

修改网卡配置为eth格式
[root@centos8 ~ ]#sed -nr '/^GRUB_CMDLINE_LINUX/s@"$@ net.ifname=0"@p' /etc/default/grub 
GRUB_CMDLINE_LINUX="resume=UUID=eac19fbe-3f3c-4f1f-804e-09458838db1c rhgb quiet net.ifname=0"
基于centos7,8的修改网卡配置
[root@centos8 ~ ]#grub2-mkconfig -o /boot/grub2/grub.cfg 
基于ubuntu的修改网卡配置
[root@centos8 ~ ]#grub-mkconfig -o /boot/grub/grub.cfg 

Guess you like

Origin blog.csdn.net/weixin_50904580/article/details/109530626