Shell脚本-2

一、grep

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

1.grep 的格式

grep  +  匹配条件  +  处理文件
例如
grep  root  passwd  找出有root的行
grep  ^root  passwd  找出以root开头的行
grep  root$  passwd  找出以root结尾的行
grep  -i  root  passwd  找出有root的行(忽略大小写)
grep  -E  "root|ROOT"  passwd
egrep  "root|ROOT" passwd
grep  -o  root  passwd
grep  -E  "\<root"  passwd
grep -E "root\>" passwd
grep -E "\<root" passwd
grep -E "\<root\>" passwd
egrep "\<root\>" passwd
grep -E "^\<root\>" passwd
grep -E "\<root\>$" passwd
grep -E -i "\<root\>" passwd
[root@host1 mnt]# vim passwd
[root@host1 mnt]# cat passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
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
test:root:test
ROOT:test:roothello
test:ROOT:test
westos:test:helloroot
westos:test:root
[root@host1 mnt]# grep root passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
test:root:test
ROOT:test:roothello
westos:test:helloroot
[root@host1 mnt]# grep -o root passwd
root
root
root
root
root
root
root
[root@host1 mnt]# grep -E "root\>" passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
test:root:test
westos:test:helloroot
[root@host1 mnt]# grep -E "\<root" passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
test:root:test
ROOT:test:roothello
[root@host1 mnt]# grep -E "\<root\>" passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
test:root:test
[root@host1 mnt]# egrep "\<root\>" passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
test:root:test
[root@host1 mnt]# grep -E "^\<root\>" passwd
root:x:0:0:root:/root:/bin/bash
[root@host1 mnt]# grep -E "\<root\>$" passwd
westos:test:root
[root@host1 mnt]# grep -E -i "\<root\>" passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
test:root:test
ROOT:test:roothello
test:ROOT:test
westos:test:root

2、grep中的正则表达式

^root   搜索以root开头的行
root$   搜索以root结尾的行
"x...y" 匹配'x...y'的行(点表示字符个数)
"x.."
"...y"
[root@host1 mnt]# vim file 
[root@host1 mnt]# cat file 
xy
xxy
xxxxy
xyyyyy
yyyyy
yyxxyyy
[root@host1 mnt]# grep -E "x..\>" file 
xxy
xxxxy
[root@host1 mnt]# grep -E "\<x.." file 
xxy
xxxxy
xyyyyy
[root@host1 mnt]# grep -E "\<x..\>" file 
xxy
[root@host1 mnt]# grep -E "y.." file 
xyyyyy
yyyyy
yyxxyyy
[root@host1 mnt]# grep -E "\<y.." file 
yyyyy
yyxxyyy
[root@host1 mnt]# grep -E "...y" file 
xxxxy
xyyyyy
yyyyy
yyxxyyy
[root@host1 mnt]# grep -E "x...y" file 
xxxxy
xyyyyy
yyxxyyy

3.grep中字符的匹配

"*"   字符出现【0-任意次】
"?"   字符出现【0-1次】
"+"   字符出现【1-任意次】
"{n}"  字符出现n次
"{m,n}" 字符最少出现m次,最多出现n次
"{0,n}" 字符出现【0-n次】
"{m,}"  字符出现至少m次
[root@host1 mnt]# vim file 
[root@host1 mnt]# cat file 
xy
xxy
xxxxy
xyyyyy
yyyyy
yyxxyyy
[root@host1 mnt]# grep -E "x*y" file 
xy
xxy
xxxxy
xyyyyy
yyyyy
yyxxyyy
[root@host1 mnt]# grep -E "x?y" file 
xy
xxy
xxxxy
xyyyyy
yyyyy
yyxxyyy
[root@host1 mnt]# grep -E "\<x?y" file 
xy
xyyyyy
yyyyy
yyxxyyy
[root@host1 mnt]# grep -E "(\<xy)?" file 
xy
xxy
xxxxy
xyyyyy
yyyyy
yyxxyyy
[root@host1 mnt]# grep -E "\<(xy)?" file 
xy
xxy
xxxxy
xyyyyy
yyyyy
yyxxyyy
[root@host1 mnt]# grep -E "\<(xy)?" file 
xy
xxy
xxxxy
xyyyyy
yyyyy
yyxxyyy
[root@host1 mnt]# grep -E "\<xy?" file 
xy
xxy
xxxxy
xyyyyy
[root@host1 mnt]# grep -E "\<x{2}y" file 
xxy
[root@host1 mnt]# grep -E "\<x{,2}y" file 
xy
xxy
xyyyyy
yyyyy
yyxxyyy
[root@host1 mnt]# grep -E "\<x{0,2}y" file 
xy
xxy
xyyyyy
yyyyy
yyxxyyy
[root@host1 mnt]# grep -E "\<x{1,2}y" file 
xy
xxy
xyyyyy
[root@host1 mnt]# grep -E "\<x{2,}y" file 
xxy
xxxxy
[root@host1 mnt]# grep -E "\<x{1,}y" file 
xy
xxy
xxxxy
xyyyyy
[root@host1 mnt]# grep -E "\<x+y" file 
xy
xxy
xxxxy
xyyyyy
[root@host1 mnt]# grep -E "xy+" file 
xy
xxy
xxxxy
xyyyyy
yyxxyyy
[root@host1 mnt]# grep -E "(xy)+" file 
xy
xxy
xxxxy
xyyyyy
yyxxyyy
[root@host1 mnt]# grep -E "(xy)+\>" file 
xy
xxy
xxxxy
[root@host1 mnt]# grep -E "\<(xy)+" file 
xy
xyyyyy
[root@host1 mnt]# 

显示eth0的IP

 #!/bin/bash
  ifconfig eth0 | grep -E "inet\>"|cut -d " " -f 10
[root@host1 mnt]# vim check_ip.sh
[root@host1 mnt]# chmod +x check_ip.sh 
[root@host1 mnt]# /mnt/check_ip.sh 
172.25.254.1

二、sed 行编辑器

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

1.Sed 命令格式

调用sed命令两种形式:
sed [options] ‘command’ file(s)
sed [options] -f scriptfile file(s)

2.sed 对字符的处理

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

P模式操作

sed -n '/UUID/p' fstab  筛选出现UUID的行并显示
sed -n '/UUID$/p' fstab  筛选以UUID开头的行并显示  
sed -n '/^UUID/p' fstab  筛选以UUID结尾的行并显示
sed -n '2,6p' -i fstab  筛选2-6行显示并倒入fstab中
sed -n '4,6!p' -i fstab 筛选除4-6行显示并倒入fstab中
sed -ne '3p;5p;6p' -i fstab  单独显示3,5,6行倒入到fstab中
[root@host1 mnt]# cat -n fstab 
     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
[root@host1 mnt]# sed -n '/UUID/p' fstab 
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
[root@host1 mnt]# sed -n '/^UUID/p' fstab 
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
[root@host1 mnt]#  sed -n '2,6p' -i fstab
[root@host1 mnt]# cat -n fstab 
     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@host1 mnt]# 
[root@host1 mnt]# cat -n fstab 
     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
[root@host1 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@host1 mnt]# cat -n fstab | sed -n '2,6!P'
     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
[root@host1 mnt]# cat -n fstab | sed -ne '3p' -ne '5p'
     3  # /etc/fstab
     5  #
[root@host1 mnt]# cat -n fstab | sed -n '3p'
     3  # /etc/fstab

d 模式操作

sed '/^UUID/d' /etc/fstab   删除以UUID开头的行
sed '/^UUID/!d' /etc/fstab      删除除以UUID开头的行
sed '/UUID$/d' fstab  删除以UUID结尾的行
sed '1,3d' fstab     删除1-3行
sed '1,3!d' fstab   删除除1-3行之外的内容
sed '1d;3d' fstab    删除1,3行
[root@host1 mnt]# cat -n fstab 
     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
[root@host1 mnt]#  sed '/^UUID/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
#
[root@host1 mnt]#  sed '/^UUID/!d' fstab 
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
[root@host1 mnt]#  sed '1,3d' 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@host1 mnt]#  sed '1,3!d' fstab 

#
# /etc/fstab

a模式

sed '/^UUID/a hello sed' fstab  添加hello sed 在以UUID开头的下一行
sed '/UUID$/a hello sed' fstab  添加hello sed 在以UUID结尾的下一行
sed '/^UUID/a hello world\n\welcom\n' fstab

(若想同时添加两行,只能同时添加,多次添加会覆盖之前添加的内容)

[root@host1 mnt]# cat -n fstab 
     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
[root@host1 mnt]# sed '/^UUID/a hello world' 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
hello world
[root@host1 mnt]# sed '/^UUID/a welcome' 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
welcome
[root@host1 mnt]# sed '/^UUID/a hello world\n\welcome\n' 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
hello world
welcome

c模式 添加

添加hello到以UUID 开头的下一行

[root@host2 mnt]# sed '/^UUID/chello' fstab 

#
# /etc/fstab
# Created by anaconda on Mon May 14 15:04:26 2018
#
# 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/mapper/rhel-root   /                       xfs     defaults        0 0
hello
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0

w 模式

sed '/^UUID/w /mnt/test' fstab  复制fstab中以UUID开头的行到test中(覆盖test)
sed '/^UUID/=' fstab  为fstab以UUID开头的行添加行号
sed '6rwestos' fstab   为fstab第六行的下一行添加westos中的内容
sed '$rwestos' fstab  为fstab最后以行的下一行添加westos中的内容
sed 'rwestos' fstab  为fstab每一行的下一行添加westos中的内容
[root@host2 mnt]# vim test

#
# /etc/fstab
# Created by anaconda on Mon May 14 15:04:26 2018
#
# 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/mapper/rhel-root   /                       xfs     defaults        0 0
UUID=1cd75bae-89a0-4d48-8239-deb4830a6fff /boot                   xfs     defaults        0 0
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
[root@host2 mnt]# cat test 
UUID=1cd75bae-89a0-4d48-8239-deb4830a6fff /boot                   xfs     defaults        0 0
[root@host2 mnt]# cat test 
UUID=1cd75bae-89a0-4d48-8239-deb4830a6fff /boot                   xfs     defaults        0 0
[root@host2 mnt]# sed '/^UUID/=' fstab 

#
# /etc/fstab
# Created by anaconda on Mon May 14 15:04:26 2018
#
# 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/mapper/rhel-root   /                       xfs     defaults        0 0
10
UUID=1cd75bae-89a0-4d48-8239-deb4830a6fff /boot                   xfs     defaults        0 0
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
[root@host2 mnt]# vim westos
[root@host2 mnt]# cat westos 
westos 123456
[root@host2 mnt]# sed '6rwestos' fstab 

#
# /etc/fstab
# Created by anaconda on Mon May 14 15:04:26 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
westos 123456
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/rhel-root   /                       xfs     defaults        0 0
UUID=1cd75bae-89a0-4d48-8239-deb4830a6fff /boot                   xfs     defaults        0 0
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
[root@host2 mnt]# sed '$rwestos' fstab 

#
# /etc/fstab
# Created by anaconda on Mon May 14 15:04:26 2018
#
# 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/mapper/rhel-root   /                       xfs     defaults        0 0
UUID=1cd75bae-89a0-4d48-8239-deb4830a6fff /boot                   xfs     defaults        0 0
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
westos 123456
[root@host2 mnt]# sed 'rwestos' fstab 

westos 123456
#
westos 123456
# /etc/fstab
westos 123456
# Created by anaconda on Mon May 14 15:04:26 2018
westos 123456
#
westos 123456
# Accessible filesystems, by reference, are maintained under '/dev/disk'
westos 123456
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
westos 123456
#
westos 123456
/dev/mapper/rhel-root   /                       xfs     defaults        0 0
westos 123456
UUID=1cd75bae-89a0-4d48-8239-deb4830a6fff /boot                   xfs     defaults        0 0
westos 123456
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
westos 123456

i模式 插入

3.sed 的其他用法

sed -n '/^UUID/=' fstab  显示以UUID开头的行的行号
sed -n -e '/^UUID/p' -e '/^UUID/=' fstab  显示UUID开头的行并显示行号
sed -e 's/mnt/file/' fstab
sed 's/\#\ //g' fstab  替换‘#’为空
sed 's/\#\ *//g' fstab 替换所有‘#’为空
sed '1,3s/\#\ *//g' fstab 替换1-3行‘#’为空
sed '/Mon/,/mount/s/\#\ *//g' fstab 替换关键词所在行‘#’为空
sed '/Mon/,/mount/s@\#\ *@@g' fstab 替换关键词所在行‘#’为空
[root@host2 mnt]# sed -n '/^UUID/=' fstab 
10
[root@host2 mnt]# sed -n -e '/^UUID/p' -e '/^UUID/=' fstab
UUID=1cd75bae-89a0-4d48-8239-deb4830a6fff /boot                   xfs     defaults        0 0
10
[root@host2 mnt]# vim rulesfile
[root@host2 mnt]# cat rulesfile 
/^UUID/p
/^UUID/=
[root@host2 mnt]# sed -f rulesfile fstab 

#
# /etc/fstab
# Created by anaconda on Mon May 14 15:04:26 2018
#
# 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/mapper/rhel-root   /                       xfs     defaults        0 0
UUID=1cd75bae-89a0-4d48-8239-deb4830a6fff /boot                   xfs     defaults        0 0
10
UUID=1cd75bae-89a0-4d48-8239-deb4830a6fff /boot                   xfs     defaults        0 0
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
[root@host2 mnt]# sed -n -f rulesfile fstab 
UUID=1cd75bae-89a0-4d48-8239-deb4830a6fff /boot                   xfs     defaults        0 0
10
[root@host2 mnt]# sed 's/\#\ //g' fstab 

#
/etc/fstab
Created by anaconda on Mon May 14 15:04:26 2018
#
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/mapper/rhel-root   /                       xfs     defaults        0 0
UUID=1cd75bae-89a0-4d48-8239-deb4830a6fff /boot                   xfs     defaults        0 0
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
[root@host2 mnt]# sed 's/\#\ *//g' fstab


/etc/fstab
Created by anaconda on Mon May 14 15:04:26 2018

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/mapper/rhel-root   /                       xfs     defaults        0 0
UUID=1cd75bae-89a0-4d48-8239-deb4830a6fff /boot                   xfs     defaults        0 0
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
[root@host2 mnt]# sed '1,3s/\#\ *//g' fstab 


/etc/fstab
# Created by anaconda on Mon May 14 15:04:26 2018
#
# 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/mapper/rhel-root   /                       xfs     defaults        0 0
UUID=1cd75bae-89a0-4d48-8239-deb4830a6fff /boot                   xfs     defaults        0 0
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
[root@host2 mnt]# sed '/Mon/,/mount/s/\#\ *//g' fstab 

#
# /etc/fstab
Created by anaconda on Mon May 14 15:04:26 2018

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/mapper/rhel-root   /                       xfs     defaults        0 0
UUID=1cd75bae-89a0-4d48-8239-deb4830a6fff /boot                   xfs     defaults        0 0
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
[root@host2 mnt]# sed '/Mon/,/mount/s@\#\ *@@g' fstab 

#
# /etc/fstab
Created by anaconda on Mon May 14 15:04:26 2018

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/mapper/rhel-root   /                       xfs     defaults        0 0
UUID=1cd75bae-89a0-4d48-8239-deb4830a6fff /boot                   xfs     defaults        0 0
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
sed 'G' redhat   每一行后加一空行
sed '$!G' redhat  除最后一行外每一行后加一空行
sed '=' redhat  每一行加行号(不在同一行)
sed -n '$p' redhat  只显示最后一行
sed '=' redhat | sed 'N;s/\n//g'  在行前加行号
sed '=' redhat | sed 'N;s/\n/ /g'  在行前加行号(有空格)
sed '=' redhat | sed 'N;s/\n/ /g' | sed -n '$p'  
[root@host2 mnt]# vim redhat
[root@host2 mnt]# cat redhat 
hello redhat
hello redhat
hello redhat
hello redhat
hello redhat
hello redhat
[root@host2 mnt]# sed 'G' redhat 
hello redhat

hello redhat

hello redhat

hello redhat

hello redhat

hello redhat

[root@host2 mnt]# sed '$!G' redhat 
hello redhat

hello redhat

hello redhat

hello redhat

hello redhat

hello redhat
[root@host2 mnt]# sed '=' redhat 
1
hello redhat
2
hello redhat
3
hello redhat
4
hello redhat
5
hello redhat
6
hello redhat
[root@host2 mnt]# sed -n '$p' fstab 
/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
[root@host2 mnt]# sed -n '$p' redhat 
hello redhat
[root@host2 mnt]# sed '=' redhat | sed 'N;s/\n//g'
1hello redhat
2hello redhat
3hello redhat
4hello redhat
5hello redhat
6hello redhat
[root@host2 mnt]# sed '=' redhat | sed 'N;s/\n/ /g'
1 hello redhat
2 hello redhat
3 hello redhat
4 hello redhat
5 hello redhat
6 hello redhat
[root@host2 mnt]# sed '=' redhat | sed 'N;s/\n/ /g' | sed -n '$p'
6 hello redhat

三、awk 报告生成器

awk 处理机制:awk 会逐行处理文本,支持在处理第一行之前做一些准备工作,以及在处理完最后一行做一些总结性质的工作,在命令格式上分别体现如下:
BEGIN{}:读入第一行文本之前执行,一般用来初始化操作
{}:逐行处理,逐行读入文本执行相应的处理,是最常见的编辑指令块
END{}:处理完最后一行文本之后执行,一般用来输出处理结果

awk 的基本用法

awk -F : 'BEGIN{print "NAME"}{ print $1 }' passwd    以冒号为分隔符开始打印NAME,再打印passwd的第一列
[root@host2 mnt]# awk -F : 'BEGIN{print "NAME"}{ print $1 }' passwd 
NAME
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
[root@host2 mnt]# awk -F : '/bash$/{ print $1 }' passwd    显示第一列并以bash结尾
root
student
**显示能够登陆系统的用户**
[root@host2 mnt]# awk -F : '/^root/{ print $1 }' passwd    显示第一列并以root开头
root
[root@host2 mnt]# awk -F : '/bash$/{ print $1 }END{print NR}' passwd  显示第一列并以bash结尾,最后显示总行数
root
student
43
[root@host2 mnt]# awk -F : '/bash$/{ print $1 }END{print NF}' passwd  显示第一列并以bash结尾,最后显示总列数
root
student
7
[root@host2 mnt]# awk -F : 'NR==3{ print $1 }' passwd    显示第三行的第一列
daemon
[root@host2 mnt]# awk -F : 'NR>=3&&NR<=6{ print $1 }' passwd   显示第三行到第六行的第一列
daemon
adm
lp
sync
[root@host2 mnt]# awk -F : 'BEGIN{N=0}/bash$/{N++}END{print NR}' passwd
43    显示以bash结尾的总行数
[root@host2 mnt]# awk -F : '/^ro/{print}' passwd    显示以ro开头的行
root:x:0:0:root:/root:/bin/bash
[root@host2 mnt]# awk -F : '/^[a-d]/{print $1,$6}' passwd  显示以a,b,c,d开头,且只显示第一列和第六列
bin /bin
daemon /sbin
adm /var/adm
dbus /
colord /var/lib/colord
abrt /etc/abrt
chrony /var/lib/chrony
avahi /var/run/avahi-daemon
apache /usr/share/httpd
[root@host2 mnt]# awk -F : 'NR>=3&&NR<=5&&/^a|nologin$/{print $1,$7}' passwd 
daemon /sbin/nologin
adm /sbin/nologin
lp /sbin/nologin
显示以分隔符‘:’分割的第三行到第五行,以a开头或以nologin结尾,显示第一列和第七列
[root@host2 mnt]# awk -F : '$6~/bin$/{print $1,$6}' passwd 
bin /bin
daemon /sbin
sync /sbin
shutdown /sbin
halt /sbin
第六列以bin结尾的行,并显示其第一列和第六列
[root@host2 mnt]# awk -F : '$6!~/^\/home/&&/bash$/{ print $1}' passwd 
root
第六列不是以/home/开头,并且以/bash结尾的行显示其第一列
[root@host2 mnt]# awk -F : 'BEGIN{N=0}$6!~/^\/home/&&/bash$/{N++}END{print N}' passwd 
1
第六列不是以/home/开头,并且以/bash结尾的行显示其总行数

抓取eth0网卡的ip

[root@host2 mnt]# ifconfig eth0 |  awk -F " " '/inet\>/{print $2}'
172.25.254.2
(以空格为分隔符,匹配关键词inet,并且以inet结尾的行的第二列)

猜你喜欢

转载自blog.csdn.net/lj_11111/article/details/80297428