美文网首页
note_6.4_egrep、wc、uniq、diff、patc

note_6.4_egrep、wc、uniq、diff、patc

作者: 人間失格_430b | 来源:发表于2019-01-30 20:36 被阅读0次

egrep

支持扩展的正则表达式实现类似于grep文本过滤功能;grep -E

NAME
       grep, egrep, fgrep - print lines matching a pattern

SYNOPSIS
       grep [OPTIONS] PATTERN [FILE...]
       grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]

OPTION:
  --color=auto:对匹配到的文本着色后高亮显示;
  -i:ignorecase,忽略字符的大小写;
  -o:仅显示匹配到的字符串本身;
  -v, --invert-match:显示不能被模式匹配到的行;
  -E:支持使用扩展的正则表达式元字符;
  -q, --quiet, --silent:静默模式,即不输出任何信息;

  -A #:after, 后#行
  -B #:before,前#行
  -C #:context,前后各#行

较grep不同-G :支持基本正则表达式


扩展正则表达式的元字符:

字符匹配:

   . :匹配任意单个字符
  [] :匹配指定范围内单个字符
    upper lower alpha digit space alnum punct
  [^]:匹配范围外的单个字符

匹配次数:

   * :匹配其前面的字符任意次;0,1,多次;
   .*:匹配任意长度的任意字符
   ?:匹配其前面的字符0次或1次;即其前面的字符是可有可无的;
   +:匹配其前面的字符1次或多次;即其面的字符要出现至少1次;
  {m}:匹配其前面的字符m次;
  {m,n}:匹配其前面的字符至少m次,至多n次;

位置锚定:

  ^:行首锚定;用于模式的最左侧;
  $:行尾锚定;用于模式的最右侧;
  \< 或 \b:词首锚定,用于单词模式的左侧;
  \> 或 \b:词尾锚定,用于单词模式的右侧;

分组及引用:

  ():分组;括号内的模式匹配到的字符会被记录于正则表达式引擎的内部变量中;
  后向引用:\1, \2, ...

  a|b:a或者b;
  C|cat:C或cat
  (c|C)at:cat或Cat

练习

1、找出/proc/meminfo文件中,所有以大写或小写S开头的行;至少有三种实现方式;

[root@localhost ~]# egrep '^[sS]' /proc/meminfo
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:              7660 kB
Slab:              63092 kB
SReclaimable:      25188 kB
SUnreclaim:        37904 kB
[root@localhost ~]# egrep '^(s|S)' /proc/meminfo
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:              7660 kB
Slab:              63092 kB
SReclaimable:      25188 kB
SUnreclaim:        37904 kB
[root@localhost ~]# egrep -i '^s' /proc/meminfo
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:              7660 kB
Slab:              63092 kB
SReclaimable:      25188 kB
SUnreclaim:        37904 kB

2、显示当前系统上root、centos或user1用户的相关信息;

[root@localhost ~]# egrep '^\<(root|centos|user1)\>' /etc/passwd
root:x:0:0:root:/root:/bin/bash
centos:x:4007:4007::/home/centos:/bin/bash
user1:x:4008:4008::/home/user1:/bin/bash

3、找出/etc/rc.d/init.d/functions文件中某单词后面跟一个小括号的行;

[root@localhost ~]# egrep '\b.*\b[(]' /etc/rc.d/init.d/functions 
checkpid() {
__kill_pids_term_kill_checkpids() {
__kill_pids_term_kill() {
__pids_var_run() {
__pids_pidof() {

4、使用echo命令输出一绝对路径,使用egrep取出基名;
正确答案:echo /etc/rc.d/init.d/functions | egrep -o '[^/]+/?$'

[root@localhost ~]# echo /etc/rc.d/init.d/functions | egrep -o '[^/]*$'
functions

不会做

进一步:取出其路径名;类似于对其执行dirname命令的结果;

[root@localhost ~]# echo /etc/rc.d/init.d/functions | egrep -o '^/.*/'
/etc/rc.d/init.d/

5、找出ifconfig命令结果中的1-255之间的数值;

[root@localhost ~]# ifconfig | egrep  '\b(2[0-4][0-9]|25[0-5]|1[0-9]{2}|[1-9][0-9]|[0-9])\b'
        inet 192.168.223.129  netmask 255.255.255.0  broadcast 192.168.223.255
        inet6 fe80::e537:3c3b:9ce6:ce37  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:ae:46:bc  txqueuelen 1000  (Ethernet)
        RX packets 20117  bytes 3452519 (3.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 7781  bytes 1083162 (1.0 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

6、课外作业:找出ifconfig命令结果中的IP地址;

[root@localhost ~]# ifconfig | egrep  -o '(2[0-4][0-9]|25[0-5]|1[0-9]{2}|[1-9][0-9]|[0-9])\.(2[0-4][0-9]|25[0-5]|1[0-9]{2}|[1-9][0-9]|[0-9])\.(2[0-4][0-9]|25[0-5]|1[0-9]{2}|[1-9][0-9]|[0-9])\.(2[0-4][0-9]|25[0-5]|1[0-9]{2}|[1-9][0-9]|[0-9])'
192.168.223.129
255.255.255.0
192.168.223.255
127.0.0.1
255.0.0.0

7、添加用户bash, testbash, basher以及nologin(其shell为/sbin/nologin);而后找出/etc/passwd文件中用户名同shell名的行;

[root@localhost ~]# useradd bash
[root@localhost ~]# useradd testbash
[root@localhost ~]# useradd basher
[root@localhost ~]# useradd -s /sbin/nologin nologin
[root@localhost ~]# egrep '(^.*\>).*\1$' /etc/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
bash:x:4009:4009::/home/bash:/bin/bash
nologin:x:4012:4012::/home/nologin:/sbin/nologin

fgrep:不支持正则表达式元字符;

当无需要用到元字符去编写模式时,使用fgrep必能更好;


文本查看及处理工具

wc, cut, sort, uniq, diff, patch

wc:word count

    wc  [OPTION]...  [FILE]...

  -l: lines
  -w:words
  -c: bytes

cut:

    cut OPTION... [FILE]...

OPTION:
  -d CHAR:以指定的字符为分隔符;
  -f FIELDS:挑选出的字段;
    #:指定的单个字段;
    #-#:连续的多个字段;
    #,#:离散的多个字段;

sort:

    sort  [OPTION]...  [FILE]...

OPTION:
  -n:基于数值大小而非字符进行排序;
  -t CHAR:指定分隔符;
  -k #:用于排序比较的字段;
  -r:逆序排序;
  -f:忽略字符大小写
  -u:重复的行只保留一份;
    复复行:连续且相同;

[root@localhost ~]# sort /etc/passwd -t : -k 3 -n
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
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
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
mariadb:x:997:995::/home/mariadb:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
gentoo:x:4003:4003::/users/gentoo:/bin/bash
fedora:x:4004:4004::/users/fedora:/bin/bash
testUser:x:4005:4005::/home/testUser:/bin/bash
testgrep:x:4006:4006::/home/testgrep:/bin/bashhh
centos:x:4007:4007::/home/centos:/bin/bash
user1:x:4008:4008::/home/user1:/bin/bash
bash:x:4009:4009::/home/bash:/bin/bash
testbash:x:4010:4010::/home/testbash:/bin/bash
basher:x:4011:4011::/home/basher:/bin/bash
nologin:x:4012:4012::/home/nologin:/sbin/nologin

uniq:报告或移除重复的行

    uniq [OPTION]... [INPUT [OUTPUT]]

OPTION:
  -c:显示每行的重复次数;
  -u:仅显示未曾重复过的行;
  -d:仅显示重复过的的行;

[root@localhost ~]# sort /etc/passwd -t : -k 3 -n | cut -d : -f7 | uniq -c
      1 /bin/bash
      4 /sbin/nologin
      1 /bin/sync
      1 /sbin/shutdown
      1 /sbin/halt
     12 /sbin/nologin
      3 /bin/bash
      1 /bin/bashhh
      5 /bin/bash
      1 /sbin/nologin
[root@localhost ~]# sort /etc/passwd -t : -k 3 -n | cut -d : -f7 | uniq -u
/bin/bash
/bin/sync
/sbin/shutdown
/sbin/halt
/bin/bashhh
/sbin/nologin
[root@localhost ~]# sort /etc/passwd -t : -k 3 -n | cut -d : -f7 | uniq -d
/sbin/nologin
/sbin/nologin
/bin/bash
/bin/bash

diff:compare files line by line

    diff [OPTION]... FILES  
    diff  /PATH/TO/OLDFILE  /PATH/TO/NEWFILE > /PATH/TO/PATCH_FILE

  -u:使用unified机制,即显示要修改的行的上下文,默认为3行;

patch:向文件打补丁

    patch [OPTIONS] -i /PATH/TO/PATCH_FILE /PATH/TO/OLDFILE
    patch /PATH/TO/OLDFILE < /PATH/TO/PATCH_FILE

  - R :反向打补丁

[root@localhost ~]# diff /etc/fstab /tmp/fstab.test 
5c5
< #
---
> # this is what i insert
[root@localhost ~]# diff -u /etc/fstab /tmp/fstab.test 
--- /etc/fstab  2019-01-08 12:35:51.281996498 -0500
+++ /tmp/fstab.test 2019-01-31 22:46:00.317895214 -0500
@@ -2,7 +2,7 @@
 #
 # /etc/fstab
 # Created by anaconda on Tue Jan  8 12:35:51 2019
-#
+# this is what i insert
 # 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@localhost ~]# diff /etc/fstab /tmp/fstab.test > /tmp/patch.test
[root@localhost ~]# cat /tmp/patch.test 
5c5
< #
---
> # this is what i insert


[root@localhost ~]# patch -Ri /tmp/patch.test  /tmp/fstab.test 
patching file /tmp/fstab.test
[root@localhost ~]# cat /tmp/fstab.test 

#
# /etc/fstab
# Created by anaconda on Tue Jan  8 12:35:51 2019
#
# 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/centos-root /                       xfs     defaults        0 0
UUID=6e0c483a-767f-4836-9722-4ee11ad0e354 /boot                   xfs     defaults        0 0
/dev/mapper/centos-home /home                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
[root@localhost ~]# patch /tmp/fstab.test < /tmp/patch.test 
patching file /tmp/fstab.test
[root@localhost ~]# cat /tmp/fstab.test 

#
# /etc/fstab
# Created by anaconda on Tue Jan  8 12:35:51 2019
# this is what i insert
# 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/centos-root /                       xfs     defaults        0 0
UUID=6e0c483a-767f-4836-9722-4ee11ad0e354 /boot                   xfs     defaults        0 0
/dev/mapper/centos-home /home                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0


练习

1、取出ifconfig ens33命令结果中的ip地址;

[root@localhost ~]# ifconfig ens33 | cut -d' ' -f10 | grep '^[0-9]*\.\+'
192.168.223.129

相关文章

网友评论

      本文标题:note_6.4_egrep、wc、uniq、diff、patc

      本文链接:https://www.haomeiwen.com/subject/mgmcsqtx.html