shell 文本处理工具

1. grep            文本过滤命令


全面搜索研究正则表达式并显示出来
grep 命令是一种强大的文本搜索工具 , 根据用户指定的“模式”对目标文本进行匹配检查 , 打印匹配到的行
由正则表达式或者字符及基本文本字符所编写的过滤条件

grep 的格式
grep     匹配条件    处理文件
grep      root      passwd
grep     ^root      passwd
grep     root$      passwd
grep -i root        passwd
grep -E "root|ROOT" passwd

[root@client ~]# cp /etc/passwd /mnt
[root@client ~]# cd /mnt
[root@client mnt]# grep root passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@client mnt]# vim passwd
[root@client mnt]# cat passwd
test:test:root
test:root:test
ROOT:test:test
[root@client mnt]# grep ^root passwd                                                #^root:以root为首
root:x:0:0:root:/root:/bin/bash
[root@client mnt]# grep root$ passwd                                               #root$:以root结尾
test:test:root
[root@client mnt]# grep -i ^root passwd                                            #-i:忽略大小写
root:x:0:0:root:/root:/bin/bash
ROOT:test:test
[root@client mnt]# grep -i root$ passwd
test:test:root
[root@client mnt]# grep -i -E "^root|root$" passwd                            #-E:扩展正则表达式,如果没有的话字符识别不了
root:x:0:0:root:/root:/bin/bash
test:test:root
ROOT:test:test
[root@client mnt]# grep -v -i -E "^root|root$" passwd |grep root      #-v:反向过滤,把符合条件的过滤掉
operator:x:11:0:operator:/root:/sbin/nologin
test:root:test
[root@client mnt]# grep -i root passwd |grep -v -i -E "^root|root$"
operator:x:11:0:operator:/root:/sbin/nologin
test:root:test

 

 

grep 中的正则表达式

^westos
westos$
'w....s'
'w.....'
'.....s'

 

 

 

 

grep 中字符的匹配次数设定

 

 

 

*                     字符出现 [0- 任意次 ]
\?                    字符出现 [0-1 次 ]
\+                    字符出现 [1- 任意次 ]
\{n\}                 字符出现 [n 次 ]
|{m,n\}             字符出现 [ 最少出现 m 次,最多出现 n 次 ]
\{0,n\}              字符出现 [0-n 次 ]
\{m,\}               字符出现 [ 至少 m 次 ]
\(xy\)\{n\}xy     关键字出现 [n 次 ]
.*                     关键字之间匹配任意字符
 

 

测试:

[root@client mnt]# grep 'r..t' test
root
[root@client mnt]# grep 'r...t' test
rooot
raaat
[root@client mnt]# grep 'r....t' test
roooot
[root@client mnt]# grep 'r*t' test
rt
rot
root
rooot
roooot
rooooot
raaat
[root@client mnt]# grep 'ro*t' test
rt
rot
root
rooot
roooot
rooooot
[root@client mnt]# grep -E 'ro?t' test
rt
rot
[root@client mnt]# grep -E 'ro{1,}t' test
rot
root
rooot
roooot
rooooot
[root@client mnt]# grep -E 'ro{1,3}t' test
rot
root
rooot
[root@client mnt]# grep -E 'ro{,3}t' test
rt
rot
root
rooot
[root@client mnt]# grep -E 'ro{3,}t' test
rooot
roooot
rooooot
[root@client mnt]# grep -E 'ro+t' test
rot
root
rooot
roooot
rooooot
[root@client mnt]# grep -E 'ro{1,}t' test
rot
root
rooot
roooot
rooooot
[root@client mnt]# vim test

rt
rot
root
rootot
rootoot
rootroot
raaat
[root@client mnt]# grep -E 'root' test
root
rootot
rootoot
rootroot
[root@client mnt]# grep -E '(root){2,}' test
rootroot
[root@client mnt]# vim test

rt
rot
root
roorrrrrrrrrrrrrt
rootoot
rootroot
raaat
[root@client mnt]# grep -E 'r.*t' test
rt
rot
root
roorrrrrrrrrrrrrt
rootoot
rootroot
raaat
[root@client mnt]# grep -E 'r*t' test
rt
rot
root
roorrrrrrrrrrrrrt
rootoot
rootroot
raaat
[root@client mnt]# grep -E "r.." test
rot
root
roorrrrrrrrrrrrrt
rootoot
rootroot
raaat
[root@client mnt]# grep -E "r..\>" test                                                  #\>:防止搜索做拓展
rot
roorrrrrrrrrrrrrt
[root@client mnt]# grep -E "...t" test
root
roorrrrrrrrrrrrrt
rootoot
rootroot
raaat
[root@client mnt]# grep -E "\<...t" test
root
rootoot
rootroot
[root@client mnt]# ifconfig eth0 | grep -E "inet\>"
        inet 172.25.254.102  netmask 255.255.255.0  broadcast 172.25.254.255

 

grep 中字符的匹配位置设定

^ 关键字
关键字 $
\< 关键字
关键字 \>
\< 关键字 \>


grep 正则表达式与扩展正则表达式
正规的 grep 不支持扩展的正则表达式子 , 竖线是用于表示”或”的扩展正则表达式元字符 , 正规的 grep 无法识别
加上反斜杠 , 这个字符就被翻译成扩展正则表达式 , 就像 grep和grep -E 一样


[root@client mnt]# grep -E "ro+t" test
rot
root
rootoot
rootroot
[root@client mnt]# grep "ro\+t" test
rot
root
rootoot
rootroot
 

 

2.  sed             行编辑器

stream editor
用来操作纯 ASCII 码的文本
处理时 , 把当前处理的行存储在临时缓冲区中 , 称为“模式空间” (pattern space) 可以指定仅仅处理哪些行
sed 符合模式条件的处理,不符合条件的不予处理,处理完成之后把缓冲区的内容送往屏幕,接着处理下一行 , 这样不断重复 , 直到文件末尾


Sed 命令格式
调用 sed 命令有两种形式:


sed [options] 'command' file(s)

sed 参数 命令 处理对象
sed [options] -f scriptfile file(s)
sed 参数  -f  处理规则 处理对象

sed 对字符的处理


p显示
d删除
a添加
c替换
w写入
i插入

 

测试:

[root@client mnt]# cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh
[root@client mnt]# grep -v nologin /etc/shells
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash
/bin/tcsh
/bin/csh
[root@client mnt]# echo `grep -v nologin /etc/shells`
/bin/sh /bin/bash /usr/bin/sh /usr/bin/bash /bin/tcsh /bin/csh
[root@client mnt]# echo `grep -v nologin /etc/shells` |sed 's/ /|/g'
/bin/sh|/bin/bash|/usr/bin/sh|/usr/bin/bash|/bin/tcsh|/bin/csh
 

 

练习1:显示本机可以登录的用户

[root@client mnt]# vim show_loginuser.sh


#!/bin/bash
SHELL=$(echo `grep -v nologin /etc/shells`|sed 's/ /|/g')
grep -E "$SHELL" /etc/passwd |cut -d : -f 1


[root@client mnt]# chmod +x show_loginuser.sh
[root@client mnt]# /mnt/show_loginuser.sh
root
student
[root@client mnt]# useradd -s /bin/tcsh user1
[root@client mnt]# su - user1
[user1@client ~]$ logout
[root@client mnt]# /mnt/show_loginuser.sh
root
student
user1




[root@client mnt]# grep nologin /etc/shells
/sbin/nologin
/usr/sbin/nologin
[root@client mnt]# echo `grep nologin /etc/shells`
/sbin/nologin /usr/sbin/nologin
[root@client mnt]# echo `grep nologin /etc/shells` |sed 's/ /###/g'
/sbin/nologin###/usr/sbin/nologin

-n:不重复显示
-e:添加两个策略   #两个策略分开写,就要给两个策略前面都加e,如果用分割符隔开加个单引就可以不用加或者前面加一个

[root@client mnt]# cp /etc/fstab /mnt
[root@client mnt]# sed -n '/^#/p' fstab                                                                                    #只显示以#为开头的行
#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# 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
#
[root@client mnt]# sed -n '/^#/!p' fstab                                                                                #不显示以#开头的行

UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
/dev/vg0/vo    /home    ext4    defaults    0 0
[root@client mnt]# sed -n '/0$/!p' fstab                                                                              #不显示以0结尾的行

#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# 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
#
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
[root@client mnt]# sed -n '/0$/p' fstab                                                                              #只显示以0结尾的行
/dev/vg0/vo    /home    ext4    defaults    0 0
[root@client mnt]# cat -n fstab |sed -n '2,6p'                                                                   #显示第二行到第六行
     2    #
     3    # /etc/fstab
     4    # Created by anaconda on Wed May  7 01:22:57 2014
     5    #
     6    # Accessible filesystems, by reference, are maintained under '/dev/disk'
[root@client mnt]# cat -n fstab |sed -n -e'2p' -e '6p'                                                      #显示第二行和第六行
     2    #
     6    # Accessible filesystems, by reference, are maintained under '/dev/disk'
[root@client mnt]# cat -n fstab |sed -n -e'2p;6p'                                                            #显示第二行和第六行
     2    #
     6    # Accessible filesystems, by reference, are maintained under '/dev/disk'
[root@client mnt]# cat -n fstab |sed -n -e'2!p'                                                                #不显示第二行
     1    
     3    # /etc/fstab
     4    # Created by anaconda on Wed May  7 01:22:57 2014
     5    #
     6    # Accessible filesystems, by reference, are maintained under '/dev/disk'
     7    # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
     8    #
     9    UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
    10    /dev/vg0/vo    /home    ext4    defaults    0 0
[root@client mnt]# cat -n fstab |sed -n -e '2!p' | sed -n -e '6!p'                                  #不显示第二行和第六行
     1    
     3    # /etc/fstab
     4    # Created by anaconda on Wed May  7 01:22:57 2014
     5    #
     6    # Accessible filesystems, by reference, are maintained under '/dev/disk'
     8    #
     9    UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
    10    /dev/vg0/vo    /home    ext4    defaults    0 0
[root@client mnt]# cat -n fstab |sed -n -e '2!p;6!p' |uniq -d                                       #不显示第二行和第六行
     1    
     3    # /etc/fstab
     4    # Created by anaconda on Wed May  7 01:22:57 2014
     5    #
     7    # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
     8    #
     9    UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
    10    /dev/vg0/vo    /home    ext4    defaults    0 0
 

 


[root@client mnt]# vim test.sh


#!/bin/bash
for i in `seq 1 10`
do
echo $i
done

[root@client mnt]# sh test.sh

练习2:读取给定的用户文件即密码文件并建立

[root@client mnt]# vim userfile

user1
user2
user3
[root@client mnt]# vim passfile

123
456
789
[root@client mnt]# vim user_create.sh


#!/bin/bash
MAX_LINE=`wc -l $1|cut -d " " -f 1`
for LINE_NUM in `seq 1 $MAX_LINE`
do
      USERNAME=`sed -n "${LINE_NUM}p" $1`
      PASSWORD=`sed -n "${LINE_NUM}p" $2`
      useradd $USERNAME
      echo $PASSWORD| passwd --stdin $USERNAME
done



[root@client mnt]# sh user_create.sh userfile passfile
Changing password for user user1.
passwd: all authentication tokens updated successfully.
Changing password for user user2.
passwd: all authentication tokens updated successfully.
Changing password for user user3.
passwd: all authentication tokens updated successfully.
[root@client mnt]# id user1
uid=1001(user1) gid=1001(user1) groups=1001(user1)
[root@client mnt]# su - user1
[user1@client ~]$ su - user2
Password:
[user2@client ~]$


d   删除

[root@client mnt]# cat -n fstab |sed -e '2d;6d'                                                              #删除第二行和第六行
     1    
     3    # /etc/fstab
     4    # Created by anaconda on Wed May  7 01:22:57 2014
     5    #
     7    # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
     8    #
     9    UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
    10    /dev/vg0/vo    /home    ext4    defaults    0 0
[root@client mnt]# cat -n fstab |sed -e '2,6d'                                                               #删除2到6行
     1    
     7    # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
     8    #
     9    UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
    10    /dev/vg0/vo    /home    ext4    defaults    0 0

[root@client mnt]# sed -e '/^#/d' fstab                             #删除#开头的所有行

UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                               xfs     defaults        1 1
/dev/vg0/vo    /home    ext4    defaults    0 0


[root@client mnt]# sed -e '/^$/d' fstab                                                                         #删除没有内容的所有行
#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# 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
#
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
/dev/vg0/vo    /home    ext4    defaults    0 0
[root@client mnt]# sed -e '/^$/d;/^#/d' fstab                                                           #删除没有内容以几以#开头的所有行
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
/dev/vg0/vo    /home    ext4    defaults    0 0
[root@client mnt]# sed -e '/UUID/d' fstab                                                               #删除以UUID开头的所有行

#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# 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
#
/dev/vg0/vo    /home    ext4    defaults    0 0
[root@client mnt]# sed -e '/UUID/!d' fstab                                                             #删除不是以UUID开头的所有行
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1


a   添加

[root@client mnt]# sed '/hello/aworld' westos
hello
world
[root@client mnt]# sed '/hello/aworld\nwestos' westos
hello
world
westos


i   插入

[root@client mnt]# sed '/hello/iworld\nwestos' westos
world
westos
hello

c   替换

[root@client mnt]# sed '/hello/chello world' westos
hello world
[root@client mnt]# sed '/hello/cwestos\nworld' westos
westos
world

w   写入

[root@client mnt]# sed -n '/bash$/p' passwd
root:x:0:0:root:/root:/bin/bash
[root@client mnt]# sed -n '/bash$/p' passwd >file
[root@client mnt]# cat file
root:x:0:0:root:/root:/bin/bash
[root@client mnt]# rm -fr file
[root@client mnt]# sed -n '/bash$/wfile' passwd
[root@client mnt]# cat file
root:x:0:0:root:/root:/bin/bash


>file与wfile
执行结果相同,不同的是>file执行两次,wfile执行一次




[root@client mnt]# cat westos
hello
[root@client mnt]# sed  '=' westos                                                                 #=:添加行号
1
hello
[root@client mnt]# echo `sed  '=' westos`
1 hello
[root@client mnt]# sed  '=' -i westos                                                               #-i:在文件中修改
[root@client mnt]# cat westos
1
hello
[root@client mnt]# sed '=' westos
1
hello
2
world



[root@client mnt]# sed '6r westos' fstab                                                         #在fstab文件第6行后插入westos文件里的内容

#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
hello
world
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
/dev/vg0/vo    /home    ext4    defaults    0 0
[root@client mnt]# sed  '$r westos' fstab                                                     #在fstab文件最后一行后插入westos文件里的内容

#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# 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
#
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
/dev/vg0/vo    /home    ext4    defaults    0 0
hello
world
[root@client mnt]# sed  '1r westos' fstab                                                  #在fstab文件第一行后插入westos文件里的内容

hello
world
#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# 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
#
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
/dev/vg0/vo    /home    ext4    defaults    0 0






[root@client mnt]# vim file

westos
hello
[root@client mnt]# vim westos

123
[root@client mnt]# sed '1rwestos' file                   #在file第一行插入westos文件内容
westos
123
hello
[root@client mnt]# sed '$r westos' file                  #在file最后一行行插入westos文件内容
westos
hello
123
[root@client mnt]# sed 'rwestos' file                    #在file每行插入westos文件内容
westos
123
hello
123
[root@client mnt]# sed 'a westos' file                   #在file文件每行后面添加westos
westos
westos
hello
westos






[root@client mnt]# sed '/^UUID/=' fstab                 #=:行号,在UUID前加上行号

#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# 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
#
9
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
/dev/vg0/vo    /home    ext4    defaults    0 0
[root@client mnt]# sed -n '/^UUID/=' fstab
9
[root@client mnt]# sed -n -e '/^UUID/p' fstab
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
[root@client mnt]# sed -n -e '/^UUID/p;/^UUID/=' fstab           #在UUID后一行加上行号
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
9
[root@client mnt]# sed '=' fstab | sed 'N;s/\n/ /g'
1
2 #
3 # /etc/fstab
4 # Created by anaconda on Wed May  7 01:22:57 2014
5 #
6 # Accessible filesystems, by reference, are maintained under '/dev/disk'
7 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
8 #
9 UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
10 /dev/vg0/vo    /home    ext4    defaults    0 0


[root@client mnt]# sed 'G' fstab                                #每一行后面加空行


#

# /etc/fstab

# Created by anaconda on Wed May  7 01:22:57 2014

#

# 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

#

UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1

/dev/vg0/vo    /home    ext4    defaults    0 0


[root@client mnt]# sed '$!G' fstab                            #除了最后一行,每行后面都加空行


#

# /etc/fstab

# Created by anaconda on Wed May  7 01:22:57 2014

#

# 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

#

UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1

/dev/vg0/vo    /home    ext4    defaults    0 0
[root@client mnt]# sed -n '$p' fstab                                  #显示最后一行
/dev/vg0/vo    /home    ext4    defaults    0 0











[root@client mnt]# sed 's/nologin/westos/g' passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/westos
daemon:x:2:2:daemon:/sbin:/sbin/westos
adm:x:3:4:adm:/var/adm:/sbin/westos
lp:x:4:7:lp:/var/spool/lpd:/sbin/westos
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/westos
operator:x:11:0:operator:/root:/sbin/westos
games:x:12:100:games:/usr/games:/sbin/westos
ftp:x:14:50:FTP User:/var/ftp:/sbin/westos
nobody:x:99:99:Nobody:/:/sbin/westos
test:test:root
test:root:test
ROOT:test:test
[root@client mnt]# sed '/adm/,/sync/s/nologin/westos/g' 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/westos
lp:x:4:7:lp:/var/spool/lpd:/sbin/westos
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
test:test:root
test:root:test
ROOT:test:test
[root@client mnt]# vim file

s/sbin/westos/g
s/nologin/linux/g
[root@client mnt]# sed -f file passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/westos/linux
daemon:x:2:2:daemon:/westos:/westos/linux
adm:x:3:4:adm:/var/adm:/westos/linux
lp:x:4:7:lp:/var/spool/lpd:/westos/linux
sync:x:5:0:sync:/westos:/bin/sync
shutdown:x:6:0:shutdown:/westos:/westos/shutdown
halt:x:7:0:halt:/westos:/westos/halt
mail:x:8:12:mail:/var/spool/mail:/westos/linux
operator:x:11:0:operator:/root:/westos/linux
games:x:12:100:games:/usr/games:/westos/linux
ftp:x:14:50:FTP User:/var/ftp:/westos/linux
nobody:x:99:99:Nobody:/:/westos/linux
test:test:root
test:root:test
ROOT:test:test
[root@client mnt]# sed -f file -i passwd                                                    #-i:在文件中修改
[root@client mnt]# cat passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/westos/linux
daemon:x:2:2:daemon:/westos:/westos/linux
adm:x:3:4:adm:/var/adm:/westos/linux
lp:x:4:7:lp:/var/spool/lpd:/westos/linux
sync:x:5:0:sync:/westos:/bin/sync
shutdown:x:6:0:shutdown:/westos:/westos/shutdown
halt:x:7:0:halt:/westos:/westos/halt
mail:x:8:12:mail:/var/spool/mail:/westos/linux
operator:x:11:0:operator:/root:/westos/linux
games:x:12:100:games:/usr/games:/westos/linux
ftp:x:14:50:FTP User:/var/ftp:/westos/linux
nobody:x:99:99:Nobody:/:/westos/linux
test:test:root
test:root:test
ROOT:test:test



练习3:写一个脚本,执行后可以直接安装httpd,并且转换为任意指定端口

[root@client mnt]# vim test.sh

#!/bin/bash
yum install httpd.x86_64 -y &> /dev/null;
sed -i "/^Listen/cListen $1" /etc/httpd/conf/httpd.conf;
echo the listen is changed;
sed -ne '42p' /etc/httpd/conf/httpd.conf
systemctl restart httpd


[root@client mnt]# sh test.sh 8080
the listen is changed
Listen 8080

 

 

3.      awk            报告生成器

 

 

 

[root@client mnt]# awk -F ":" '{print $1}' passwd                                            #F:指定分隔符,显示passwd文件的第一列
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
test
test
ROOT
[root@client mnt]# awk -F ":" 'BEGIN{print "name"}{print $1}' passwd                          #在开始显示name
name
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
test
test
ROOT
[root@client mnt]# awk -F ":" 'BEGIN{print "NAME"}{print $1}END{print "END"}' passwd         #在开始显示NAME,结尾显示END
NAME
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
test
test
ROOT
END
[root@client mnt]# awk -F ":" 'BEGIN{print "NAME"}{print NR}END{print "END"}' passwd        #NR:行号
NAME
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
END

[root@client mnt]# awk -F ":" 'BEGIN{print "NAME"}{print NR$1}END{print "END"}' passwd
NAME
1root
2bin
3daemon
4adm
5lp
6sync
7shutdown
8halt
9mail
10operator
11games
12ftp
13nobody
14test
15test
16ROOT
END



[root@client mnt]# awk '/bash$/{print}' passwd
root:x:0:0:root:/root:/bin/bash
[root@client mnt]# awk -F ":" '/bash$/{print $1}' passwd
root


练习4:列出存在的用户
[root@client mnt]# awk -F : 'BEGIN{N=0}/bash$/{N++}END{print N}' passwd
1




[root@client mnt]# awk '/^ro/{print}' passwd                 #
root:x:0:0:root:/root:/bin/bash
[root@client mnt]# awk '/^[a-d]/{print}' passwd
bin:x:1:1:bin:/bin:/westos/linux
daemon:x:2:2:daemon:/westos:/westos/linux
adm:x:3:4:adm:/var/adm:/westos/linux
[root@client mnt]# awk '/^[^a-d]/{print}' passwd
root:x:0:0:root:/root:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/westos/linux
sync:x:5:0:sync:/westos:/bin/sync
shutdown:x:6:0:shutdown:/westos:/westos/shutdown
halt:x:7:0:halt:/westos:/westos/halt
mail:x:8:12:mail:/var/spool/mail:/westos/linux
operator:x:11:0:operator:/root:/westos/linux
games:x:12:100:games:/usr/games:/westos/linux
ftp:x:14:50:FTP User:/var/ftp:/westos/linux
nobody:x:99:99:Nobody:/:/westos/linux
test:test:root
test:root:test
ROOT:test:test
[root@client mnt]# awk '/^a|nologin$/{print}' passwd
adm:x:3:4:adm:/var/adm:/westos/linux
[root@client mnt]# awk '/^r&&/bash$/{print}' passwd
[root@client mnt]# awk '/^r/||/bash$/{print}' passwd
root:x:0:0:root:/root:/bin/bash
[root@client mnt]# awk -F : '$5~/^a/{print}' passwd
adm:x:3:4:adm:/var/adm:/westos/linux
[root@client mnt]# awk -F : '$1~/^r/{print}' passwd
root:x:0:0:root:/root:/bin/bash
[root@client mnt]# awk -F ":" '$1!~/^r/{print}' passwd
bin:x:1:1:bin:/bin:/westos/linux
daemon:x:2:2:daemon:/westos:/westos/linux
adm:x:3:4:adm:/var/adm:/westos/linux
lp:x:4:7:lp:/var/spool/lpd:/westos/linux
sync:x:5:0:sync:/westos:/bin/sync
shutdown:x:6:0:shutdown:/westos:/westos/shutdown
halt:x:7:0:halt:/westos:/westos/halt
mail:x:8:12:mail:/var/spool/mail:/westos/linux
operator:x:11:0:operator:/root:/westos/linux
games:x:12:100:games:/usr/games:/westos/linux
ftp:x:14:50:FTP User:/var/ftp:/westos/linux
nobody:x:99:99:Nobody:/:/westos/linux
test:test:root
test:root:test
ROOT:test:test
[root@client mnt]# awk -F ":" '$6!~/home/&&/bash$/{print $1}' /etc/passwd
root
[root@client mnt]# awk -F ":" '$7!~/bash$/{print}' passwd
bin:x:1:1:bin:/bin:/westos/linux
daemon:x:2:2:daemon:/westos:/westos/linux
adm:x:3:4:adm:/var/adm:/westos/linux
lp:x:4:7:lp:/var/spool/lpd:/westos/linux
sync:x:5:0:sync:/westos:/bin/sync
shutdown:x:6:0:shutdown:/westos:/westos/shutdown
halt:x:7:0:halt:/westos:/westos/halt
mail:x:8:12:mail:/var/spool/mail:/westos/linux
operator:x:11:0:operator:/root:/westos/linux
games:x:12:100:games:/usr/games:/westos/linux
ftp:x:14:50:FTP User:/var/ftp:/westos/linux
nobody:x:99:99:Nobody:/:/westos/linux
test:test:root
test:root:test
ROOT:test:test
[root@client mnt]# awk -F ":" '{print NR,$0}' passwd
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/westos/linux
3 daemon:x:2:2:daemon:/westos:/westos/linux
4 adm:x:3:4:adm:/var/adm:/westos/linux
5 lp:x:4:7:lp:/var/spool/lpd:/westos/linux
6 sync:x:5:0:sync:/westos:/bin/sync
7 shutdown:x:6:0:shutdown:/westos:/westos/shutdown
8 halt:x:7:0:halt:/westos:/westos/halt
9 mail:x:8:12:mail:/var/spool/mail:/westos/linux
10 operator:x:11:0:operator:/root:/westos/linux
11 games:x:12:100:games:/usr/games:/westos/linux
12 ftp:x:14:50:FTP User:/var/ftp:/westos/linux
13 nobody:x:99:99:Nobody:/:/westos/linux
14 test:test:root
15 test:root:test
16 ROOT:test:test
[root@client mnt]# awk -F ":" '$6!~/home/&&/bash$/{print}' /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@client mnt]# awk -F ":" '$6!~/home/&&/bash$/{print $1}' /etc/passwd
root
[root@client mnt]# awk -F ":" 'BEGIN{n=0}$6!~/^\/home/&&/bash$/{n++}END{print n}' /etc/passwd
1
[root@client mnt]# ifconfig eth0 |awk '/inet\>/{print $2}'
172.25.254.102
[root@client mnt]# awk 'BEGIN{n=0}{n++}END{print n}' /etc/passwd
44




$():标准拓展型接口相当于``
$[]=(()) 运算

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值