grep,sed,awk工具的使用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了grep,sed,awk工具的使用相关的知识,希望对你有一定的参考价值。

grep过滤特定的行

1.把包含‘halt’的行以及这行下面的两行都打印出来。

[[email protected] ~]# cp /etc/passwd /tmp/

[[email protected] ~]# cd /tmp/

[[email protected] tmp]# grep -A2 ‘halt‘ passwd

halt:x:7:0:halt:/sbin:/sbin/halt

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

[[email protected] tmp]#

2.把包含‘halt’的行以及这行上面的两行都打印出来。

[[email protected] tmp]# grep -B2 ‘halt‘ passwd

sync:x:5:0:sync:/sbin:/bin/sync

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

halt:x:7:0:halt:/sbin:/sbin/halt

[[email protected] tmp]#

3.把包含‘halt’的行以及这行上面和下面的各两行都打印出来。

[[email protected] tmp]# grep -C2 ‘halt‘ passwd

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

uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

[[email protected] tmp]#

4.过滤出带有关键词‘root’的行并输出行号

[[email protected] tmp]# grep -n ‘root‘ passwd

1:root:x:0:0:root:/root:/bin/bash

11:operator:x:11:0:operator:/root:/sbin/nologin

[[email protected] tmp]#

5.过滤不带关键词‘nologin’的行并输出行号

[[email protected] tmp]# grep -nv ‘nologin‘ passwd

1:root:x:0:0:root:/root:/bin/bash

6:sync:x:5:0:sync:/sbin:/bin/sync

7:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

8:halt:x:7:0:halt:/sbin:/sbin/halt

21:user1:x:500:500::/tmp/rachy:/bin/bash

22:user12:x:501:501::/home/user12:/bin/bash

23:rachy:x:502:502::/home/rachy:/bin/bash

24:Jack:x:503:503::/home/Jack:/bin/bash

[[email protected] tmp]#

6.过滤出所有包含数字的行

[[email protected] tmp]# cp /etc/inittab .

[[email protected] tmp]# grep ‘[0-9]‘ inittab

# upstart works, see init(5), init(8), and initctl(8).

#   0 - halt (Do NOT set initdefault to this)

#   1 - Single user mode

#   2 - Multiuser, without NFS (The same as 3, if you do not have networking)

#   3 - Full multiuser mode

#   4 - unused

#   5 - X11

#   6 - reboot (Do NOT set initdefault to this)

id:3:initdefault:

[[email protected] tmp]#

7.过滤出所有不含数字的行

[[email protected] tmp]# grep -v ‘[0-9]‘ inittab

# inittab is only used by upstart for the default runlevel.

#

# ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.

#

# System initialization is started by /etc/init/rcS.conf

#

# Individual runlevels are started by /etc/init/rc.conf

#

# Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf

#

# Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf,

# with configuration in /etc/sysconfig/init.

#

# For information on how to write upstart event handlers, or how

#

# Default runlevel. The runlevels used are:

#

[[email protected] tmp]#

8.过滤出所有不以“#”开头的行

[[email protected] tmp]# grep -v ‘^#‘ inittab

id:3:initdefault:

[[email protected] tmp]#

9.去掉所有空行和以“#”开头的行

[[email protected] tmp]# cp /etc/crontab .

[[email protected] tmp]# grep -v ‘^#‘ crontab |grep -v ‘^$‘

SHELL=/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO=root

HOME=/

[[email protected] tmp]#

在正则表达式中“^”表示行的开始,“$”表示行的结尾,那么空行就可以用“^$”表示。

10.打印出不以英文字母开头的行

[[email protected] tmp]# cat test.txt

123

abc

456

abc2323

#laksdjf

Allllll

[[email protected] tmp]# grep ‘^[^a-zA-Z]‘ test.txt #方法1,不包括空行

123

456

#laksdjf

[[email protected] tmp]# grep -v ‘^[a-zA-Z]‘ test.txt #方法2,包括空行(更合适)

123

456

#laksdjf

[[email protected] tmp]#

11.含有非英文字符的行

[[email protected] tmp]# grep ‘[^a-zA-Z]‘ test.txt #方法1,不含空行

123

456

abc2323

#laksdjf

[[email protected] tmp]# grep -v ‘^[a-zA-Z]\+$‘ test.txt #方法2,包括空行(更合适)

123

456

abc2323

#laksdjf

[[email protected] tmp]#

12.过滤含有r..o的行,点表示一个任意字符

[[email protected] tmp]# grep ‘r..o‘ passwd

operator:x:11:0:operator:/root:/sbin/nologin

gopher:x:13:30:gopher:/var/gopher:/sbin/nologin

vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin

[[email protected] tmp]#

13.过滤含有ooo*的行,星表示前面字符重复0次或多次,即oo,ooo,oooo,……

[[email protected] tmp]# grep ‘ooo*‘ passwd

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

uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

operator:x:11:0:operator:/root:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

[[email protected] tmp]#

14.统计passwd文件的行数

[[email protected] tmp]# grep ‘.*‘ passwd |wc -l #方法1,.*表示任意字符重复任意多次

24

[[email protected] tmp]# wc -l passwd #方法2,使用wc命令更简单

24 passwd

[[email protected] tmp]#

15.指定要过滤字符出现的次数

[[email protected] tmp]# grep ‘o\{2\}‘ passwd

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

uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

operator:x:11:0:operator:/root:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

[[email protected] tmp]#

下面我们来试试egrep

1.筛选一个或多个前面的字符

[[email protected] tmp]# cat test.txt

rot:x:0:0:rot:/rot:/bin/bash

roooot:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/root:/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

1234422798776

dasaddssdfjhalkja

[[email protected] tmp]# egrep ‘o+‘ test.txt #过滤含有至少一个o的行

rot:x:0:0:rot:/rot:/bin/bash

roooot:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/root:/sbin/nologin

adm:x:3:4:adm:/var/adm:/sbin/nologin

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

[[email protected] tmp]# egrep ‘oo+‘ test.txt #过滤含有至少两个o的行

roooot:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/root:/sbin/nologin

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

[[email protected] tmp]#

2.筛选零个或一个前面的字符

[[email protected] tmp]# egrep ‘o?‘ test.txt #过滤含有0个或1个o的行

rot:x:0:0:rot:/rot:/bin/bash

roooot:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/root:/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

1234422798776

dasaddssdfjhalkja

[[email protected] tmp]# egrep ‘ooo?‘ test.txt #过滤含有两个或三个o的行

roooot:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/root:/sbin/nologin

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

[[email protected] tmp]#

3.筛选字符串1或字符串2

[[email protected] tmp]# egrep ‘sbin|22|nologin‘ test.txt

roooot:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/root:/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

1234422798776

[[email protected] tmp]#

4.egrep中()的应用

[[email protected] tmp]# egrep ‘r(oo)|(at)o‘ test.txt

root:x:0:0:root:/root:/bin/bash

rooot:x:0:0:root:/root:/bin/bash

root:x:0:0:roooooooot:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

[[email protected] tmp]#

5.用()表示一个整体,例如(oo)+表示1个或多个‘oo’

[[email protected] tmp]# egrep ‘(oo)+‘ test.txt

root:x:0:0:root:/root:/bin/bash

rooot:x:0:0:root:/root:/bin/bash

root:x:0:0:roooooooot:/root:/bin/bash

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

operator:x:11:0:operator:/root:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

[[email protected] tmp]#

sed工具的使用

1.打印指定的行

[[email protected] tmp]# sed -n ‘2‘p passwd

bin:x:1:1:bin:/bin:/sbin/nologin

[[email protected] tmp]#

2.打印所有行

[[email protected] tmp]# sed -n ‘1,$‘p passwd

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

uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

operator:x:11:0:operator:/root:/sbin/nologin

games:x:12:100:games:/usr/games:/sbin/nologin

gopher:x:13:30:gopher:/var/gopher:/sbin/nologin

ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

nobody:x:99:99:Nobody:/:/sbin/nologin

dbus:x:81:81:System message bus:/:/sbin/nologin

vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin

saslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

user1:x:500:500::/tmp/rachy:/bin/bash

user12:x:501:501::/home/user12:/bin/bash

rachy:x:502:502::/home/rachy:/bin/bash

Jack:x:503:503::/home/Jack:/bin/bash

[[email protected] tmp]#

3.可以指定一个区间

[[email protected] tmp]# sed -n ‘1,3‘p passwd

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin

[[email protected] tmp]#

4.打印包含指定字符串的行

[[email protected] tmp]# sed -n ‘/root/‘p passwd

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

[[email protected] tmp]#

5.grep中使用的特殊字符,如^$.*等同样也能在sed中使用

[[email protected] tmp]# sed -n ‘/^r/‘p passwd

root:x:0:0:root:/root:/bin/bash

rachy:x:502:502::/home/rachy:/bin/bash

[[email protected] tmp]# sed -n ‘/in$/‘p 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

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

operator:x:11:0:operator:/root:/sbin/nologin

games:x:12:100:games:/usr/games:/sbin/nologin

gopher:x:13:30:gopher:/var/gopher:/sbin/nologin

ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

nobody:x:99:99:Nobody:/:/sbin/nologin

dbus:x:81:81:System message bus:/:/sbin/nologin

vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin

saslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

[[email protected] tmp]# sed -n ‘/r..o/‘p passwd

operator:x:11:0:operator:/root:/sbin/nologin

gopher:x:13:30:gopher:/var/gopher:/sbin/nologin

vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin

[[email protected] tmp]# sed -n ‘/oo*/‘p passwd

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

uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

operator:x:11:0:operator:/root:/sbin/nologin

games:x:12:100:games:/usr/games:/sbin/nologin

gopher:x:13:30:gopher:/var/gopher:/sbin/nologin

ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

nobody:x:99:99:Nobody:/:/sbin/nologin

dbus:x:81:81:System message bus:/:/sbin/nologin

vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin

saslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

user12:x:501:501::/home/user12:/bin/bash

rachy:x:502:502::/home/rachy:/bin/bash

Jack:x:503:503::/home/Jack:/bin/bash

6.使用-e选项可以实现多个行为

[[email protected] tmp]# sed -e ‘1‘p -e ‘/rachy/‘p -n passwd

root:x:0:0:root:/root:/bin/bash

user1:x:500:500::/tmp/rachy:/bin/bash

rachy:x:502:502::/home/rachy:/bin/bash

[[email protected] tmp]#

7.删除一行或多行

[[email protected] tmp]# sed ‘1‘d 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

uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

operator:x:11:0:operator:/root:/sbin/nologin

games:x:12:100:games:/usr/games:/sbin/nologin

gopher:x:13:30:gopher:/var/gopher:/sbin/nologin

ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

nobody:x:99:99:Nobody:/:/sbin/nologin

dbus:x:81:81:System message bus:/:/sbin/nologin

vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin

saslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

user1:x:500:500::/tmp/rachy:/bin/bash

user12:x:501:501::/home/user12:/bin/bash

rachy:x:502:502::/home/rachy:/bin/bash

Jack:x:503:503::/home/Jack:/bin/bash

[[email protected] tmp]# sed ‘1,3‘d passwd

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

uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

operator:x:11:0:operator:/root:/sbin/nologin

games:x:12:100:games:/usr/games:/sbin/nologin

gopher:x:13:30:gopher:/var/gopher:/sbin/nologin

ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

nobody:x:99:99:Nobody:/:/sbin/nologin

dbus:x:81:81:System message bus:/:/sbin/nologin

vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin

saslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

user1:x:500:500::/tmp/rachy:/bin/bash

user12:x:501:501::/home/user12:/bin/bash

rachy:x:502:502::/home/rachy:/bin/bash

Jack:x:503:503::/home/Jack:/bin/bash

[[email protected] tmp]# sed ‘/root/‘d 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

uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

games:x:12:100:games:/usr/games:/sbin/nologin

gopher:x:13:30:gopher:/var/gopher:/sbin/nologin

ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

nobody:x:99:99:Nobody:/:/sbin/nologin

dbus:x:81:81:System message bus:/:/sbin/nologin

vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin

saslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

user1:x:500:500::/tmp/rachy:/bin/bash

user12:x:501:501::/home/user12:/bin/bash

rachy:x:502:502::/home/rachy:/bin/bash

Jack:x:503:503::/home/Jack:/bin/bash

[[email protected] tmp]#

8.替换字符或字符串

[[email protected] tmp]# sed ‘1,2s/root/rachy/g‘ passwd

rachy:x:0:0:rachy:/rachy:/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

uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

operator:x:11:0:operator:/root:/sbin/nologin

games:x:12:100:games:/usr/games:/sbin/nologin

gopher:x:13:30:gopher:/var/gopher:/sbin/nologin

ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

nobody:x:99:99:Nobody:/:/sbin/nologin

dbus:x:81:81:System message bus:/:/sbin/nologin

vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin

saslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

user1:x:500:500::/tmp/rachy:/bin/bash

user12:x:501:501::/home/user12:/bin/bash

rachy:x:502:502::/home/rachy:/bin/bash

Jack:x:503:503::/home/Jack:/bin/bash

[[email protected] tmp]# sed ‘s#nologin#login#g‘ passwd

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/login

daemon:x:2:2:daemon:/sbin:/sbin/login

adm:x:3:4:adm:/var/adm:/sbin/login

lp:x:4:7:lp:/var/spool/lpd:/sbin/login

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/login

uucp:x:10:14:uucp:/var/spool/uucp:/sbin/login

operator:x:11:0:operator:/root:/sbin/login

games:x:12:100:games:/usr/games:/sbin/login

gopher:x:13:30:gopher:/var/gopher:/sbin/login

ftp:x:14:50:FTP User:/var/ftp:/sbin/login

nobody:x:99:99:Nobody:/:/sbin/login

dbus:x:81:81:System message bus:/:/sbin/login

vcsa:x:69:69:virtual console memory owner:/dev:/sbin/login

saslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/login

postfix:x:89:89::/var/spool/postfix:/sbin/login

sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/login

user1:x:500:500::/tmp/rachy:/bin/bash

user12:x:501:501::/home/user12:/bin/bash

rachy:x:502:502::/home/rachy:/bin/bash

Jack:x:503:503::/home/Jack:/bin/bash

[[email protected] tmp]# sed ‘[email protected]@[email protected]‘ passwd

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/false

daemon:x:2:2:daemon:/sbin:/sbin/false

adm:x:3:4:adm:/var/adm:/sbin/false

lp:x:4:7:lp:/var/spool/lpd:/sbin/false

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/false

uucp:x:10:14:uucp:/var/spool/uucp:/sbin/false

operator:x:11:0:operator:/root:/sbin/false

games:x:12:100:games:/usr/games:/sbin/false

gopher:x:13:30:gopher:/var/gopher:/sbin/false

ftp:x:14:50:FTP User:/var/ftp:/sbin/false

nobody:x:99:99:Nobody:/:/sbin/false

dbus:x:81:81:System message bus:/:/sbin/false

vcsa:x:69:69:virtual console memory owner:/dev:/sbin/false

saslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/false

postfix:x:89:89::/var/spool/postfix:/sbin/false

sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/false

user1:x:500:500::/tmp/rachy:/bin/bash

user12:x:501:501::/home/user12:/bin/bash

rachy:x:502:502::/home/rachy:/bin/bash

Jack:x:503:503::/home/Jack:/bin/bash

[[email protected] tmp]# sed ‘s/[0-9]//g‘ passwd

root:x:::root:/root:/bin/bash

bin:x:::bin:/bin:/sbin/nologin

daemon:x:::daemon:/sbin:/sbin/nologin

adm:x:::adm:/var/adm:/sbin/nologin

lp:x:::lp:/var/spool/lpd:/sbin/nologin

sync:x:::sync:/sbin:/bin/sync

shutdown:x:::shutdown:/sbin:/sbin/shutdown

halt:x:::halt:/sbin:/sbin/halt

mail:x:::mail:/var/spool/mail:/sbin/nologin

uucp:x:::uucp:/var/spool/uucp:/sbin/nologin

operator:x:::operator:/root:/sbin/nologin

games:x:::games:/usr/games:/sbin/nologin

gopher:x:::gopher:/var/gopher:/sbin/nologin

ftp:x:::FTP User:/var/ftp:/sbin/nologin

nobody:x:::Nobody:/:/sbin/nologin

dbus:x:::System message bus:/:/sbin/nologin

vcsa:x:::virtual console memory owner:/dev:/sbin/nologin

saslauth:x:::"Saslauthd user":/var/empty/saslauth:/sbin/nologin

postfix:x::::/var/spool/postfix:/sbin/nologin

sshd:x:::Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

user:x::::/tmp/rachy:/bin/bash

user:x::::/home/user:/bin/bash

rachy:x::::/home/rachy:/bin/bash

Jack:x::::/home/Jack:/bin/bash

[[email protected] tmp]# sed ‘s/[a-zA-Z]//g‘ passwd

::0:0::/://

::1:1::/://

::2:2::/://

::3:4:://://

::4:7::///://

::5:0::/://

::6:0::/://

::7:0::/://

::8:12::///://

::10:14::///://

::11:0::/://

::12:100:://://

::13:30:://://

::14:50: ://://

::99:99::/://

::81:81:  :/://

::69:69:   :/://

::499:76:" ":///://

::89:89::///://

::74:74:- :///://

1::500:500:://://

12::501:501:://12://

::502:502:://://

::503:503:://://

[[email protected] tmp]#

9.调换两个字符串的位置

[[email protected] tmp]# sed ‘s/\(root\)\(.*\)\(bash\)/\3\2\1/‘ passwd

bash:x:0:0:root:/root:/bin/root

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

uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

operator:x:11:0:operator:/root:/sbin/nologin

games:x:12:100:games:/usr/games:/sbin/nologin

gopher:x:13:30:gopher:/var/gopher:/sbin/nologin

ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

nobody:x:99:99:Nobody:/:/sbin/nologin

dbus:x:81:81:System message bus:/:/sbin/nologin

vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin

saslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

user1:x:500:500::/tmp/rachy:/bin/bash

user12:x:501:501::/home/user12:/bin/bash

rachy:x:502:502::/home/rachy:/bin/bash

Jack:x:503:503::/home/Jack:/bin/bash

[[email protected] tmp]# sed ‘s/^.*$/123&/‘ passwd

123root:x:0:0:root:/root:/bin/bash

123bin:x:1:1:bin:/bin:/sbin/nologin

123daemon:x:2:2:daemon:/sbin:/sbin/nologin

123adm:x:3:4:adm:/var/adm:/sbin/nologin

123lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

123sync:x:5:0:sync:/sbin:/bin/sync

123shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

123halt:x:7:0:halt:/sbin:/sbin/halt

123mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

123uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

123operator:x:11:0:operator:/root:/sbin/nologin

123games:x:12:100:games:/usr/games:/sbin/nologin

123gopher:x:13:30:gopher:/var/gopher:/sbin/nologin

123ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

123nobody:x:99:99:Nobody:/:/sbin/nologin

123dbus:x:81:81:System message bus:/:/sbin/nologin

123vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin

123saslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin

123postfix:x:89:89::/var/spool/postfix:/sbin/nologin

123sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

123user1:x:500:500::/tmp/rachy:/bin/bash

123user12:x:501:501::/home/user12:/bin/bash

123rachy:x:502:502::/home/rachy:/bin/bash

123Jack:x:503:503::/home/Jack:/bin/bash

10.直接修改文件内容

[[email protected] tmp]# sed -i ‘s/root/rachy/g‘ passwd

[[email protected] tmp]# cat passwd

rachy:x:0:0:rachy:/rachy:/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

uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

operator:x:11:0:operator:/rachy:/sbin/nologin

games:x:12:100:games:/usr/games:/sbin/nologin

gopher:x:13:30:gopher:/var/gopher:/sbin/nologin

ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

nobody:x:99:99:Nobody:/:/sbin/nologin

dbus:x:81:81:System message bus:/:/sbin/nologin

vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin

saslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

user1:x:500:500::/tmp/rachy:/bin/bash

user12:x:501:501::/home/user12:/bin/bash

rachy:x:502:502::/home/rachy:/bin/bash

Jack:x:503:503::/home/Jack:/bin/bash

[[email protected] tmp]#

sed练习题:

1.把/etc/passwd复制到/tmp/test.txt,用sed打印所有行

[[email protected] tmp]# cp /etc/passwd /tmp/test.txt

[[email protected] tmp]# sed -n p test.txt #方法1

[[email protected] tmp]# sed -n ‘1,$‘p test.txt #方法2

2.打印test.txt的3到10行

[[email protected] tmp]# sed -n ‘3,10‘p test.txt

3.打印test.txt中包含root的行

[[email protected] tmp]# sed -n ‘/root/‘p test.txt

4.删除test.txt中15行及以后所有行

[[email protected] tmp]# sed ‘15,$‘d test.txt

5.删除test.txt中包含bash的行

[[email protected] tmp]# sed ‘/bash/‘d test.txt

6.替换test.txt中的root为rachy

[[email protected] tmp]# sed ‘s/root/rachy/g‘ test.txt

7.替换test.txt中的/sbin/nologin为/bin/login

[[email protected] tmp]# sed ‘s#/sbin/nologin#/bin/login#g‘ test.txt #方法1

[[email protected] tmp]# sed ‘[email protected]/sbin/[email protected]/bin/login#g‘ test.txt #方法2

[[email protected] tmp]# sed ‘s/\/sbin\/nologin/\/bin\/login/g‘ test.txt #方法3

8.删除test.txt中5到10行所有数字

[[email protected] tmp]# sed ‘5,10s/[0-9]//g‘ test.txt

9.删除test.txt所有特殊字符(除了数字和大小写字母)

[[email protected] tmp]# sed ‘s/[^0-9a-zA-Z]//g‘ test.txt

10.把test.txt中第一个单词和最后一个单词调换位置

[[email protected] tmp]# sed ‘s/\(^[a-zA-Z]\+\)\([^a-zA-Z].*\)\([^a-zA-Z]\)\([a-zA-Z]*$\)/\4\2\3\1/‘ test.txt #方法1

[[email protected] tmp]# sed ‘s/\(^[a-zA-Z][a-zA-Z]*\)\([^a-zA-Z].*\)\([^a-zA-Z]\)\([a-zA-Z]*$\)/\4\2\3\1/‘ test.txt #方法2

11.把test.txt中出现的第一个数字和最后一个单词互换位置

[[email protected] tmp]# sed ‘s/\([^0-9][^0-9]*\)\([0-9][0-9]*\)\([^0-9].*\)\([^a-zA-Z]\)\([a-zA-Z][a-zA-Z]*$\)/\1\5\3\4\2/‘ test.txt #方法1

[[email protected] tmp]# sed ‘s/\([^0-9]*\)\([0-9]\+\)\([^0-9].*\)\([^0-9a-zA-Z]\)\([a-zA-Z]\+$\)/\1\5\3\4\2/‘ test.txt #方法2

12.把test.txt中第一个数字移动到行末尾

[[email protected] tmp]# sed ‘s/\([^0-9][^0-9]*\)\([0-9]*\)\([^0-9][^0-9]*$\)/\1\3\2/‘ test.txt

[[email protected] tmp]# sed ‘s/\([^0-9]*\)\([0-9]\+\)\([^0-9].*$\)/\1\3\2/‘ test.txt

13.在test.txt中20行到末行最前面加aaa:

[[email protected] tmp]# sed ‘20,$s/^.*$/aaa:&/‘ test.txt

awk工具的使用

1.截取文档中的某个段

[[email protected] tmp]# head -n 2 passwd |awk -F ‘:‘ ‘{print $1}‘

root

bin

[[email protected] tmp]# head -n 2 passwd |awk -F ‘:‘ ‘{print $0}‘

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

[[email protected] tmp]# head -n 2 passwd |awk -F ‘:‘ ‘{print $1"#"$2"#"$3}‘

root#x#0

bin#x#1

[[email protected] tmp]#

2.匹配字符或字符串

[[email protected] tmp]# awk ‘/oo/‘ 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

uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

operator:x:11:0:operator:/root:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

[[email protected] tmp]# awk -F ‘:‘ ‘$1~/oo/‘ test.txt

root:x:0:0:root:/root:/bin/bash

[[email protected] tmp]# awk -F ‘:‘ ‘/root/ {print $1,$3} /rachy/ {print $1,$3}‘ passwd

root 0

operator 11

user1 500

rachy 502

[[email protected] tmp]#

3.条件操作符

[[email protected] tmp]# awk -F ‘:‘ ‘$3=="0"‘ passwd

root:x:0:0:root:/root:/bin/bash

[[email protected] tmp]# awk -F ‘:‘ ‘$3==0‘ passwd

root:x:0:0:root:/root:/bin/bash

[[email protected] tmp]# awk -F ‘:‘ ‘$3>="500"‘ passwd

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

nobody:x:99:99:Nobody:/:/sbin/nologin

dbus:x:81:81:System message bus:/:/sbin/nologin

vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

user1:x:500:500::/tmp/rachy:/bin/bash

user12:x:501:501::/home/user12:/bin/bash

rachy:x:502:502::/home/rachy:/bin/bash

Jack:x:503:503::/home/Jack:/bin/bash

[[email protected] tmp]# awk -F ‘:‘ ‘$7!="/sbin/nologin"‘ passwd

root:x:0:0:root:/root:/bin/bash

sync:x:5:0:sync:/sbin:/bin/sync

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

halt:x:7:0:halt:/sbin:/sbin/halt

user1:x:500:500::/tmp/rachy:/bin/bash

user12:x:501:501::/home/user12:/bin/bash

rachy:x:502:502::/home/rachy:/bin/bash

Jack:x:503:503::/home/Jack:/bin/bash

[[email protected] tmp]# awk -F ‘:‘ ‘$3<$4‘ passwd

adm:x:3:4:adm:/var/adm:/sbin/nologin

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

games:x:12:100:games:/usr/games:/sbin/nologin

gopher:x:13:30:gopher:/var/gopher:/sbin/nologin

ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

[[email protected] tmp]# awk -F ‘:‘ ‘$3>"5" && $3<"7"‘ passwd

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin

user1:x:500:500::/tmp/rachy:/bin/bash

user12:x:501:501::/home/user12:/bin/bash

rachy:x:502:502::/home/rachy:/bin/bash

Jack:x:503:503::/home/Jack:/bin/bash

[[email protected] tmp]# awk -F ‘:‘ ‘$3>"5" || $7=="/bin/bash"‘ passwd

root:x:0:0:root:/root:/bin/bash

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

nobody:x:99:99:Nobody:/:/sbin/nologin

dbus:x:81:81:System message bus:/:/sbin/nologin

vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

user1:x:500:500::/tmp/rachy:/bin/bash

user12:x:501:501::/home/user12:/bin/bash

rachy:x:502:502::/home/rachy:/bin/bash

Jack:x:503:503::/home/Jack:/bin/bash

4.awk的内置变量

[[email protected] tmp]# head -n3 passwd |awk -F ‘:‘ ‘{print NF}‘

7

7

7

[[email protected] tmp]# head -n3 passwd |awk -F ‘:‘ ‘{print $NF}‘

/bin/bash

/sbin/nologin

/sbin/nologin

[[email protected] tmp]# head -n3 passwd |awk -F ‘:‘ ‘{print NR}‘

1

2

3

[[email protected] tmp]# awk ‘NR>20‘ passwd

user1:x:500:500::/tmp/rachy:/bin/bash

user12:x:501:501::/home/user12:/bin/bash

rachy:x:502:502::/home/rachy:/bin/bash

Jack:x:503:503::/home/Jack:/bin/bash

[[email protected] tmp]# awk ‘NR>20 && $1~/rachy/‘ passwd

user1:x:500:500::/tmp/rachy:/bin/bash

rachy:x:502:502::/home/rachy:/bin/bash

[[email protected] tmp]#

5.awk中的数学运算

[[email protected] tmp]# head -n3 passwd |awk -F ‘:‘ ‘$1="root"‘

root x 0 0 root /root /bin/bash

root x 1 1 bin /bin /sbin/nologin

root x 2 2 daemon /sbin /sbin/nologin

[[email protected] tmp]# head -n2 passwd

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

[[email protected] tmp]# head -n2 passwd |awk -F ‘:‘ ‘{$7=$3+$4}‘

[[email protected] tmp]# head -n2 passwd |awk -F ‘:‘ ‘{$7=$3+$4;print $0}‘

root x 0 0 root /root 0

bin x 1 1 bin /bin 2

[[email protected] tmp]# awk -F ‘:‘ ‘{(tol=tol+$3)};END {print tol}‘ passwd

3013

[[email protected] tmp]# awk -F ‘:‘ ‘{if($1=="root")print $0}‘ passwd

root:x:0:0:root:/root:/bin/bash

[[email protected] tmp]#

awk练习题:

1.用awk打印整个test.txt

[[email protected] tmp]# awk ‘{print $0}‘ passwd

2.查找所有包含bash的行

[[email protected] tmp]# awk ‘/bash/‘ test.txt

3.用“:”作分隔符,查找第三段等于0的行

[[email protected] tmp]# awk -F ‘:‘ ‘$3==0‘ test.txt

4.用“:”作分隔符,查找第一段为“root”的行,并把该段的“root”换成“toor”(可以连同sed一起使用)

[[email protected] tmp]# awk -F ‘:‘ ‘$1=="root"‘ test.txt |sed ‘s/root/toor/‘

5.用“:”作分隔符,打印最后一段

[[email protected] tmp]# awk -F ‘:‘ ‘{print $NF}‘ test.txt

6.打印行数大于20的所有行

[[email protected] tmp]# awk -F ‘:‘ ‘NR>20‘ test.txt

7.用“:”作分隔符,打印所有第三段小于第四段的行

[[email protected] tmp]# awk -F ‘:‘ ‘$3<$4‘ test.txt

8.用“:”作分隔符,打印第一段以及最后一段,并且中间用@连接

[[email protected] tmp]# awk -F ‘:‘ ‘{print $1"@"$NF}‘ test.txt #方法1

[[email protected] tmp]# awk -F ‘:‘ ‘OFS="@" {print $1,$NF}‘ test.txt #方法2

9.用“:”作分隔符,把整个文档的第四段相加,求和

[[email protected] tmp]# awk -F ‘:‘ ‘{sum=sum+$4};END {print sum}‘ test.txt


以上是关于grep,sed,awk工具的使用的主要内容,如果未能解决你的问题,请参考以下文章

Unix & Linux的文本处理工具 -- grep, sed & awk

Shell文本处理工具(Linux三剑客 grep sed awk )

linux三大文件处理工具(grep/sed/awk)

文本处理工具之:grep sed awk

linux--补充(管道| / 重定向> / xargs)/find 与xargs结合使用/vi,grep,sed,awk(支持正则表达式的工具程序)

shell 编程四剑客简介 find sed grep awk(微信公众号摘抄)