GREP文本过滤命令:
一.命令结构:
grep 匹配条件 处理文本
二.命令内容:
【实验环境】:在desktop虚拟机中进行实验
[root@localhost mnt]# cp /etc/passwd /mnt/ ##复制passwd到/mnt下进行实验
[root@localhost mnt]# ls
passwd
【grep root passwd】 ##查看passwd中root字符行
[root@localhost mnt]# grep root passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
【grep ^root passwd】 ##查看passwd中以root开头的字符行
[root@localhost mnt]# grep ^root passwd
root:x:0:0:root:/root:/bin/bash
【grep root$ passwd】 ##查看passwd中以root结尾的字符行
[root@localhost mnt]# grep root$ passwd
texy.tysd.root
【grep -i ^root passwd】 ##忽略大小写查看passwd中以root开头的字符行
[root@localhost mnt]# grep -i ^root passwd
root:x:0:0:root:/root:/bin/bash
ROOT.KKJJ.LL
【grep -i -E “^root|root$” passwd】 ##查看passwd中以root开头和结尾所有的字符行
[root@localhost mnt]# grep -i -E "^root|root$" passwd
root:x:0:0:root:/root:/bin/bash
texy.tysd.root
ROOT.KKJJ.LL
【grep -i root passwd | grep -v -i -E】 ##查看passwd中以root在中间所有的字符行
[root@localhost mnt]# grep -i root passwd | grep -v -i -E "^root|root$"
operator:x:11:0:operator:/root:/sbin/nologin
text:root:test
1.grep中字符的匹配次数设定:
* 字符出现[0-任意次]
\? 字符出现[0-1次]
\+ 字符出现[1-任意次]
\{n} 字符出现[n次]
\{m,n} 字符出现[最少出现m次,最多出现n次]
\{0,n} 字符出现[0-n次]
\{m,} 字符出现[至少m次]
\{xy}{n}xy 关键字出现[n次]
* 关键字之间匹配任意字符
【实验过程】:
[root@localhost mnt]# vim test ##新建一个文件
[root@localhost mnt]# cat test ##查看编辑内容
rt
rot
root
rooot
roooot
rootroot
rootoot
[root@localhost mnt]# grep 'r*t' test ##r和t之间出现任意字符出现
rt
rot
root
rooot
roooot
[root@localhost mnt]# grep -E 'ro*t' test ##o出现0-任意次
rt
rot
root
rooot
roooot
[root@localhost mnt]# grep -E 'ro?t' test ##o出现0-1次
rt
rot
[root@localhost mnt]# grep -E 'ro{1,}t' test ##o出现1-任意次
rot
root
rooot
roooot
[root@localhost mnt]# grep -E 'ro{1,3}t' test ##o出现1-3次
rot
root
rooot
[root@localhost mnt]# grep -E 'ro{,3}t' test ##o出现0-3次
rt
rot
root
rooot
[root@localhost mnt]# grep -E '(root){2,}' test ##显示两个以上的root行
rootroot
rootoot
[root@localhost mnt]# grep -E 'r*t' test ###表示r从零到所有的行
rt
rot
root
rooot
roooot
rootroot
rootoot
[root@localhost mnt]# grep -E 'r.*t' test ##表示r和t之间的任意字符
rt
rot
root
rooot
roooot
rootroot
rootoot
2.grep中字符的匹配位置设定:
^关键字
关键字$
\<关键字
关键字>
\<关键字>
[root@localhost mnt]# grep -E 'r....' test ##表示r开头后面有五个字符的行
[root@localhost mnt]# grep -E 'r....\>' test ##表示过滤r开头后面有五个字符不扩展行
[root@localhost mnt]# grep -E '\<....r' test ##表示过滤t结尾前面有五个字符不扩展行
rootroot
3.编写一个shell脚本列出本机可登陆用户:
【信息查看】:
[root@localhost mnt]# cat /etc/shells ##查看文件
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh
[root@localhost mnt]# grep -v nologin /etc/shells ##把有nologin的行显示出来
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash
/bin/tcsh
/bin/csh
[root@localhost mnt]# echo `grep -v nologin /etc/shells` ##将列的信息改为行
/bin/sh /bin/bash /usr/bin/sh /usr/bin/bash /bin/tcsh /bin/csh
[root@localhost mnt]# echo `grep -v nologin /etc/shells` |sed 's/ /|/g' ##去掉行间空格
/bin/sh|/bin/bash|/usr/bin/sh|/usr/bin/bash|/bin/tcsh|/bin/csh
【脚本编写】:
[root@localhost 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@localhost mnt]# chmod +x show_loginuser.sh ##加权限
【实验测试】:
[root@localhost mnt]# /mnt/show_loginuser.sh
root
student ##有两个用户可以直接登录
[root@localhost mnt]# useradd -s /bin/tcsh user1 ##建立user1用户
[root@localhost mnt]# su - user1
[user1@localhost ~]$ logout
[root@localhost mnt]# /mnt/show_loginuser.sh ##查看
root
student
user1 ##user1添加到脚本中
SED行编辑器:
一.sed的命令格式:
1.调用sed命令有两种形式:
sed [options] ‘command’file(s)
sed [options] -f scriptfile file(s)
2.sed 对字符的处理:
p 显示
d 删除
a 添加
c 替换
w 写入
i 插入
二.sed命令具体用法:
【实验环境】:
[root@localhost mnt]# cp /etc/fstab /mnt/ ##将fstab文件复制到/mnt下
[root@localhost mnt]# ls
fstab passwd show_loginuser.sh test
[root@localhost mnt]# rm -fr passwd ##删除之前建立的文件
[root@localhost mnt]# rm -fr test
[root@localhost mnt]# rm -fr show_loginuser.sh
[root@localhost mnt]# ls ##查看环境
fstab
1.p模式操作:
[root@localhost mnt]# sed -n '/^#/p' fstab ##显示以#开头的行
[root@localhost mnt]# sed -n '/^#/!p' fstab ##显示除了以#开头的行
[root@localhost mnt]# sed -n '/0$/p' fstab ##显示以0结尾的行
[root@localhost mnt]# cat -n fstab
[root@localhost mnt]# cat -n fstab |sed -n '2,6p' ##显示2-6行
[root@localhost mnt]# cat -n fstab |sed -n -e '2p' -e '6p' ##显示第二行和第六行(-e表示多个条件)
[root@localhost mnt]# cat -n fstab |sed -n -e '2p;6p' ##显示第二行和第六行
[root@localhost mnt]# cat -n fstab |sed -ne '2!p;6!p' | uniq -d ##显示除了第二行和第六行
2.d操作模式;
[root@localhost mnt]# sed -e '2d;6d' fstab ##除了2和6行都显示
[root@localhost mnt]# sed -e '/^#/d' fstab ##删除以#开头的行
[root@localhost mnt]# sed -e '/^$/d' fstab ##删除空格行
[root@localhost mnt]# sed -e '/^$/d;/^#/d' fstab ##删除空格行和#开头的行
3.a操作模式:
[root@localhost mnt]# vim westos
hello
[root@localhost mnt]# sed '/hello/aworld' westos
hello
world ##在hello后面添加
[root@localhost mnt]# sed '/hello/aworld\nwestos' westos ##\n在world后面换行添加
hello
world
westos
[root@localhost mnt]# sed 's/hello/hello world/g' westos ##将hello更换成hello world
hello world
4.i操作模式:
[root@localhost mnt]# sed '/hello/iwestos' westos
westos
hello
5.c操作模式:
[root@localhost mnt]# sed '/hello/chello world' westos
[root@localhost mnt]# cp /etc/passwd .
6.w操作模式:
[root@localhost mnt]# sed -n '/bash$/p' passwd > file
[root@localhost mnt]# cat file
[root@localhost mnt]# rm -fr file
[root@localhost mnt]# sed -n '/bash$/wfile' passwd
[root@localhost mnt]# cat file
注意:w和>的区别:w做的是一个命令,>做的是两个命令;w的效率要比>高
[root@localhost mnt]# sed '/hello/p' westos
[root@localhost mnt]# sed '/hello/=' westos
1
hello
[root@localhost mnt]# echo `sed '/hello/=' westos`
1 hello
[root@localhost mnt]# sed '6r westos' fstab ##将westos的内容加到fstab文件的第六行下面
7.sed其他用法:
[root@localhost mnt]# sed -n '/^UUID/=' fstab ##只显示行数
9
[root@localhost mnt]# sed '/^UUID/=' fstab ##显示行数和内容
9
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 / xfs defaults 1 1
[root@localhost mnt]# sed '=' fstab | sed 'N;s/\n//g' ##在文件前面加行号
[root@localhost mnt]# sed '=' fstab | sed 'N;s/\n/ /g' ##内容和行号之间有空格
[root@localhost mnt]# sed 'G' fstab ##在内容的每一行后面加一个空行
[root@localhost mnt]# sed '$!G' fstab ##在最后一行不加空行
[root@localhost mnt]# sed -n '$p' fstab ##显示最后一行
8.s替换模式:
[root@localhost mnt]# sed 's/nologin/westos/g' passwd ##将全文的nologin替换成westos(/g是替换全文)
[root@localhost mnt]# sed '3,5s/nologin/westos/g' passwd ##替换3和5行
[root@localhost mnt]# sed '/adm/,/sync/s/nologin/westos/g' passwd ##替换关键字adm到sync之间的内容
[root@localhost mnt]# sed -e '/adm/,/sync/s/nologin/westos/g;s/sbin/dmf/g' passwd ##多天指令同时执行
[root@localhost mnt]# vim file
s/sbin/westos/g
s/nologin/linux/g
[root@localhost mnt]# sed -f file passwd ##执行file里的替换命令
[root@localhost mnt]# cat passwd ##所有的替换都不会改变原文件
[root@localhost mnt]# sed -f file -i passwd ##将原文件改变
9.编写一个shell脚本同时建立三个用户:
【编辑实验文件】:
[root@localhost mnt]# vim userfile ##用户名称
user1
user2
user3
[root@localhost mnt]# vim passfile ##用户密码
user1123
user2123
user3123
【编写脚本】:
[root@localhost 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@localhost mnt]# sh user_create.sh userfile passfile ##执行脚本
useradd: user 'user1' already exists
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@localhost mnt]# id user1
uid=1001(user1) gid=1001(user1) groups=1001(user1)
[root@localhost mnt]# id user2
uid=1002(user2) gid=1002(user2) groups=1002(user2)
[root@localhost mnt]# id user3
uid=1003(user3) gid=1003(user3) groups=1003(user3)
AWK报告生成器:
一.awk概述:
awk处理机制:逐行处理文本,支持在处理第一行之前做一些准备工作,以及在处理完最后一行做一些总结性质的工作
二.awk参数:
以:为分隔符,处理前打印 NAME ,打印第1列:
awk -F : 'BEGIN{print "NAME"}{print $1}' passwd
以:为分隔符,处理前打印 NAME ,打印第1列,处理后打印行数(NF列):
awk -F : 'BEGIN{print "NAME"}{print $1}END{print NR}' passwd
以:为分隔符,打印以bash结尾行的第7列:
awk -F : '/bash$/{print $7}' passwd
以:为分隔符,打印以bash结尾行:
awk -F : '/bash$/' passwd
以:为分隔符,打印第3行:
awk -F : 'NR==3' passwd
以:为分隔符,处理前打印 NAME ,打印2-3行的第1个字符:
awk -F : 'BEGIN{print "NAME"}NR<=3&&NR>=2{print $1}' passwd
输出34加上12的运算结果:
awk 'BEGIN{a=34;print a+12}'
打印出以ro开头的行:
awk -F :'/^ro/{print}' /etc/passwd
打印出开头除了a-d的行:
awk -F :'/^[a-d]/{print $1,$6}' passwd.txt
打印出开头为a结尾为nologin的结尾行:
awk -F :'/^a|nologin$/{print }' passwd.txt
三.实验:
1.打印能登录系统的用户个数:
[root@localhost mnt]# awk -F : 'BEGIN{N=0}/bash$/{N++}END{print N}' passwd ##N++ 每有一个用户N的值加一次
4
2.打印能登陆系统且家目录不是/home的用户个数:
[root@localhost mnt]# awk -F : 'BEGIN{N=0}/$6~/^/home/&&/bash$/{N++}END{print N}' /etc/passwd
1
3.打印设备eth0的IP:
[root@localhost mnt]# ifconfig eth0 |awk '/inet\>/{print $2}'
172.25.254.120