六周第二次课
9.4/9.5 sed
grep只能实现查找功能,而不能把查找的内容替换。vim可以查找和替换,但只限于在文本内部操作,而不能输出到屏幕上。sed和awk都是流式编辑器,是针对文档的行来操作的。
sed和grep相比,也能实现grep的功能,但是很麻烦,也没有颜色显示。但是,sed有自己更加牛逼的地方,它的强项在于替换,替换一些指定的字符。
sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换、删除、新增、选取等特定工作,下面先了解一下sed的用法 sed命令行格式为:
sed [-nefri] ‘command’ 输入文本
常用选项:
-n∶使用安静(silent)模式。-n选项的作用是只显示我们要打印的行,无关紧要的内容不显示。
[root sed]# sed -n '/root/'p test.txt
root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root sed]#[root sed]# sed -n '/r.t/'p test.txt
operator:x:11:0:operator:/root:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin[root sed]# sed -n '/r*t/'p test.txt root:x:0:0:root:/root:/bin/bash shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt operator:x:11:0:operator:/root:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin ods:x:999:998:softhsm private keys owner:/var/lib/softhsm:/sbin/nologin pegasus:x:66:65:tog-pegasus OpenPegasus WBEM/CIM services:/var/lib/Pegasus:/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 polkitd:/:/sbin/nologin apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin tomcat:x:91:91:Apache Tomcat:/usr/share/tomcat:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin libstoragemgmt:x:995:993:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin unbound:x:388:388:Unbound DNS resolver:/etc/unbound:/sbin/nologin pkiuser:x:17:17:Certificate System:/usr/share/pki:/sbin/nologin setroubleshoot:x:387:387::/var/lib/setroubleshoot:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin pcp:x:383:380:Performance Co-Pilot:/var/lib/pcp:/sbin/nologin gnome-initial-setup:x:380:377::/run/gnome-initial-setup/:/sbin/nologin avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin dovenull:x:379:376:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin oprofile:x:16:16:Special user account to be used by OProfile:/var/lib/oprofile:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin tianqi:x:1000:1000:tianqi:/home/tianqi:/bin/bash [root sed]#-r∶支持不用脱义符号
sed是支持使用+的,但是要使用脱义符号,如果不想用脱义符号,可以加-r选项。
[root@localhost sed]# sed -n '/o\+t/'p test.txt
root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin setroubleshoot:x:387:387::/var/lib/setroubleshoot:/sbin/nologin pcp:x:383:380:Performance Co-Pilot:/var/lib/pcp:/sbin/nologin dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin dovenull:x:379:376:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin[root@localhost sed]# sed -nr '/o+t/'p test.txt root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin setroubleshoot:x:387:387::/var/lib/setroubleshoot:/sbin/nologin pcp:x:383:380:Performance Co-Pilot:/var/lib/pcp:/sbin/nologin dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin dovenull:x:379:376:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin [root@localhost sed]#下面匹配2次o。
[root@localhost sed]# sed -nr '/o{2}/'p test.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 setroubleshoot:x:387:387::/var/lib/setroubleshoot:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin [root@localhost sed]#使用|或者符号
[root@localhost sed]# sed -nr '/root|bus/'p test.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 [root@localhost sed]#直接打印第2行
[root@localhost sed]# sed -n '2'p test.txt
bin:x:1:1:bin:/bin:/sbin/NOlogin打印2到5行 [root@localhost sed]# sed -n '2,5'p test.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 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin [root@localhost sed]#打印45到最后一行
[root@localhost sed]# sed -n '45,$'p test.txt
gdm:x:42:42::/var/lib/gdm:/sbin/nologin rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin pcp:x:383:380:Performance Co-Pilot:/var/lib/pcp:/sbin/nologin kdcproxy:x:382:379:IPA KDC Proxy User:/:/sbin/nologin ipaapi:x:381:378:IPA Framework User:/:/sbin/nologin gnome-initial-setup:x:380:377::/run/gnome-initial-setup/:/sbin/nologin avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin dovenull:x:379:376:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin oprofile:x:16:16:Special user account to be used by OProfile:/var/lib/oprofile:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin tianqi:x:1000:1000:tianqi:/home/tianqi:/bin/bash aming:x:1001:1001::/home/aming:/bin/bash user1:x:1002:1002::/home/user1:/bin/bash [root@localhost sed]#打印出全部内容,使用命令:
[root@localhost sed]# sed -n '1,$'p test.txt
sed命令加上-e选项可以实现多个行为,这些行为会依次执行,可能会出现重复显示。
[root@localhost sed]# sed -e '1'p -e '/bus/'p -n test.txt
root:x:0:0:root:/root:/bin/bash dbus:x:81:81:System message bus:/:/sbin/nologin [root@localhost sed]#[root@localhost sed]# sed -e '1'p -e '/root/'p -n test.txt
root:x:0:0:root:/root:/bin/bash root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@localhost sed]#[root@localhost sed]# sed -e '1'p -e '/root/'p -e '/oo*/'p -n test.txt
root:x:0:0:root:/root:/bin/bash 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 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/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 ods:x:999:998:softhsm private keys owner:/var/lib/softhsm:/sbin/nologin pegasus:x:66:65:tog-pegasus OpenPegasus WBEM/CIM services:/var/lib/Pegasus:/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 polkitd:/:/sbin/nologin apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin tomcat:x:91:91:Apache Tomcat:/usr/share/tomcat:/sbin/nologin colord:x:997:994:User for colord:/var/lib/colord:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin libstoragemgmt:x:995:993:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin dirsrv:x:389:389:user for 389-ds-base:/usr/share/dirsrv:/sbin/nologin rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin hsqldb:x:96:96::/var/lib/hsqldb:/sbin/nologin unbound:x:388:388:Unbound DNS resolver:/etc/unbound:/sbin/nologin pkiuser:x:17:17:Certificate System:/usr/share/pki:/sbin/nologin setroubleshoot:x:387:387::/var/lib/setroubleshoot:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin qemu:x:107:107:qemu user:/:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin radvd:x:75:75:radvd user:/:/sbin/nologin chrony:x:386:384::/var/lib/chrony:/sbin/nologin named:x:25:25:Named:/var/named:/sbin/nologin mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin geoclue:x:385:382:User for geoclue:/var/lib/geoclue:/sbin/nologin sssd:x:384:381:User for sssd:/:/sbin/nologin gdm:x:42:42::/var/lib/gdm:/sbin/nologin rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin pcp:x:383:380:Performance Co-Pilot:/var/lib/pcp:/sbin/nologin kdcproxy:x:382:379:IPA KDC Proxy User:/:/sbin/nologin ipaapi:x:381:378:IPA Framework User:/:/sbin/nologin gnome-initial-setup:x:380:377::/run/gnome-initial-setup/:/sbin/nologin avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin dovenull:x:379:376:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin oprofile:x:16:16:Special user account to be used by OProfile:/var/lib/oprofile:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin tianqi:x:1000:1000:tianqi:/home/tianqi:/bin/bash aming:x:1001:1001::/home/aming:/bin/bash user1:x:1002:1002::/home/user1:/bin/bash [root@localhost sed]#-Ip:不区分大小写
[root@localhost sed]# sed -n '/bus/'Ip test.txt
dbus:x:81:81:System message bus:/:/sbin/nologin [root@localhost sed]#sed命令加上d:删除指定行。参数d表示删除的动作,仅仅是在显示器屏幕上并不显示这些行而已,文档不受影响。
[root@localhost sed]# sed '1,50'd test.txt
gnome-initial-setup:x:380:377::/run/gnome-initial-setup/:/sbin/nologin avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin dovenull:x:379:376:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin oprofile:x:16:16:Special user account to be used by OProfile:/var/lib/oprofile:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin tianqi:x:1000:1000:tianqi:/home/tianqi:/bin/bash aming:x:1001:1001::/home/aming:/bin/bash user1:x:1002:1002::/home/user1:/bin/bash [root@localhost sed]#[root@localhost sed]# wc -l test.txt
61 test.txt [root@localhost sed]#sed命令加上-i:删除指定行
[root@localhost sed]# cp test.txt test.txt.bak [root@localhost sed]# sed -i '1,50'd test.txt [root@localhost sed]# wc -l test.txt
11 test.txt [root@localhost sed]#[root@localhost sed]# cat test.txt
gnome-initial-setup:x:380:377::/run/gnome-initial-setup/:/sbin/nologin avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin dovenull:x:379:376:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin oprofile:x:16:16:Special user account to be used by OProfile:/var/lib/oprofile:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin tianqi:x:1000:1000:tianqi:/home/tianqi:/bin/bashaming:x:1001:1001::/home/aming:/bin/bash user1:x:1002:1002::/home/user1:/bin/bash[root@localhost sed]# sed -i '/aming/'d test.txt [root@localhost sed]# cat test.txt
gnome-initial-setup:x:380:377::/run/gnome-initial-setup/:/sbin/nologin avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin dovenull:x:379:376:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin oprofile:x:16:16:Special user account to be used by OProfile:/var/lib/oprofile:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin tianqi:x:1000:1000:tianqi:/home/tianqi:/bin/bash user1:x:1002:1002::/home/user1:/bin/bash [root@localhost sed]#替换字符或字符串
参数s表示替换的动作,参数g表示全局替换,如果不加g则只替换本行出现的第一个,用户和vim的替换一样
[root@localhost sed]# sed '1,10s/root/toor/g' test.txt
toor:x:0:0:toor:/toor:/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:/toor:/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 ods:x:999:998:softhsm private keys owner:/var/lib/softhsm:/sbin/nologin pegasus:x:66:65:tog-pegasus OpenPegasus WBEM/CIM services:/var/lib/Pegasus:/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 polkitd:/:/sbin/nologin apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin tomcat:x:91:91:Apache Tomcat:/usr/share/tomcat:/sbin/nologin colord:x:997:994:User for colord:/var/lib/colord:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin libstoragemgmt:x:995:993:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin dirsrv:x:389:389:user for 389-ds-base:/usr/share/dirsrv:/sbin/nologin rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin hsqldb:x:96:96::/var/lib/hsqldb:/sbin/nologin unbound:x:388:388:Unbound DNS resolver:/etc/unbound:/sbin/nologin amandabackup:x:33:6:Amanda user:/var/lib/amanda:/bin/bash pkiuser:x:17:17:Certificate System:/usr/share/pki:/sbin/nologin setroubleshoot:x:387:387::/var/lib/setroubleshoot:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin qemu:x:107:107:qemu user:/:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin radvd:x:75:75:radvd user:/:/sbin/nologin chrony:x:386:384::/var/lib/chrony:/sbin/nologin named:x:25:25:Named:/var/named:/sbin/nologin mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin geoclue:x:385:382:User for geoclue:/var/lib/geoclue:/sbin/nologin sssd:x:384:381:User for sssd:/:/sbin/nologin gdm:x:42:42::/var/lib/gdm:/sbin/nologin rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin pcp:x:383:380:Performance Co-Pilot:/var/lib/pcp:/sbin/nologin kdcproxy:x:382:379:IPA KDC Proxy User:/:/sbin/nologin ipaapi:x:381:378:IPA Framework User:/:/sbin/nologin gnome-initial-setup:x:380:377::/run/gnome-initial-setup/:/sbin/nologin avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin dovenull:x:379:376:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin oprofile:x:16:16:Special user account to be used by OProfile:/var/lib/oprofile:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin tianqi:x:1000:1000:tianqi:/home/tianqi:/bin/bash aming:x:1001:1001::/home/aming:/bin/bash user1:x:1002:1002::/home/user1:/bin/bash [root@localhost sed]#要替换的字符串是可以使用正则表达式的,如.、*、+等等。
[root@localhost sed]# sed -r '1,10s/ro+/r/g' test.txt | head
rt:x:0:0:rt:/rt:/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:/rt:/sbin/nologin [root@localhost sed]#():小括号在sed中属于特殊符号,用()把想要替换的字符打包成了一个整体,替换时则写成类似\1、\2或\3的形式。
(.*)是贪婪匹配,会一直匹配到最后一个:
下面替换test.txt前10行的第一段和最后一段。因为每一行都是以:分割的,非冒号字符匹配第一段,贪婪匹配匹配中间所有段,非冒号字符匹配最后一段。
[root@localhost sed]# head test.txt | sed -r 's/([^:]+):(.*):([^:]+)/\3:\2:\1/' /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[root@localhost sed]#除了可以使用 / 作为分隔符外,我们还可以使用其他特殊符号作为分隔符,如#和@
[root@localhost sed]# head test.txt | sed 's@/sbin/nologin@123@g'
root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/NOlogin daemon:x:2:2:daemon:/sbin:123 adm:x:3:4:adm:/var/adm:123 lp:x:4:7:lp:/var/spool/lpd:123 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:123 operator:x:11:0:operator:/root:123 [root@localhost sed]#删除各行的字母,删除的意思是用空格来替换
[root@localhost sed]# head test.txt | sed 's/[a-zA-Z]//g'
::0:0::/:// ::1:1::/:// ::2:2::/:// ::3:4:://:// ::4:7::///:// ::5:0::/:// ::6:0::/:// ::7:0::/:// ::8:12::///:// ::11:0::/:// [root@localhost sed]#在所有行前面加上固定的字符串
&:匹配需要替换的内容,(.*)表示一整行。
[root@localhost sed]# head test.txt | sed -r 's/(^.*$)/aaa:&/'
aaa:root:x:0:0:root:/root:/bin/bash aaa:bin:x:1:1:bin:/bin:/sbin/NOlogin aaa:daemon:x:2:2:daemon:/sbin:/sbin/nologin aaa:adm:x:3:4:adm:/var/adm:/sbin/nologin aaa:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin aaa:sync:x:5:0:sync:/sbin:/bin/sync aaa:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown aaa:halt:x:7:0:halt:/sbin:/sbin/halt aaa:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin aaa:operator:x:11:0:operator:/root:/sbin/nologin [root@localhost sed]#
友情链接: