shell 正则表达式

shell 正则表达式

1.显示/proc/meminfo文件中以大小s开头的行(要求两种方法)

[root@server ~]# grep "^[sS]" /proc/meminfo 
SwapCached:          800 kB
SwapTotal:       2097148 kB
SwapFree:        2091004 kB
Shmem:             16036 kB
Slab:             165308 kB
SReclaimable:      67900 kB
SUnreclaim:        97408 kB
ShmemHugePages:        0 kB
ShmemPmdMapped:        0 kB
[root@server ~]# grep -i '^s' /proc/meminfo
SwapCached:          800 kB
SwapTotal:       2097148 kB
SwapFree:        2091004 kB
Shmem:             16036 kB
Slab:             165316 kB
SReclaimable:      67900 kB
SUnreclaim:        97416 kB
ShmemHugePages:        0 kB
ShmemPmdMapped:        0 kB

2.显示/etc/passwd文件中不以/bin/bash结尾的行

[root@server ~]# grep -v "/bin/bash" /etc/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
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:65534:65534:Kernel Overflow User:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin
tss:x:59:59:Account used for TPM access:/dev/null:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
unbound:x:997:994:Unbound DNS resolver:/etc/unbound:/sbin/nologin
sssd:x:996:993:User for sssd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
rngd:x:995:992:Random Number Generator Daemon:/var/lib/rngd:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
nginx:x:994:991:Nginx web server:/var/lib/nginx:/sbin/nologin
mysql:x:27:27:MySQL Server:/var/lib/mysql:/sbin/nologin
zabbix:x:993:990:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin

3.显示用户rpc默认的shell程序

[root@server ~]# cat /etc/passwd|grep 'rpc' |cut -d ':' -f7 
/sbin/nologin
/sbin/nologin

4.找出/etc/passwd中的两位或三位数------ grep -o 只显示匹配的部分

[root@server ~]# grep -o '[0-9]\{2,3\}' /etc/passwd
12
11
12
100
14
50
655
34
655
34
81
81
999
997
193
193
59
59
998
996
997
994
996
993
74
74
995
992
48
48
994
991
27
27
993
990
89
89
100
100
32
32
992
989

5.显示centos8的/etc/grub2.cfg文件中,至少以一个空白字符开头且后面有非空白字符的行

[root@server ~]# grep '^[[:space:]].\+[^[:space:]]' /etc/grub2.cfg
  load_env -f ${config_directory}/grubenv
  load_env
   set default="${next_entry}"
   set next_entry=
   save_env next_entry
   set boot_once=true
   set default="${saved_entry}"
  menuentry_id_option="--id"
  menuentry_id_option=""
  set saved_entry="${prev_saved_entry}"
  save_env saved_entry
  set prev_saved_entry=
  save_env prev_saved_entry
  set boot_once=true
  if [ -z "${boot_once}" ]; then
    saved_entry="${chosen}"
    save_env saved_entry
  fi
  if [ x$feature_all_video_module = xy ]; then
    insmod all_video
  else
    insmod efi_gop
    insmod efi_uga
    insmod ieee1275_fb
    insmod vbe
    insmod vga
    insmod video_bochs
    insmod video_cirrus
  fi
  set timeout_style=menu
  set timeout=5
  set timeout=5
  source ${prefix}/user.cfg
  if [ -n "${GRUB2_PASSWORD}" ]; then
    set superusers="root"
    export superusers
    password_pbkdf2 root ${GRUB2_PASSWORD}
  fi
  # if countdown has ended, choose to boot rollback deployment,
  # i.e. default=1 on OSTree-based systems.
  if  [ "${boot_counter}" = "0" -o "${boot_counter}" = "-1" ]; then
    set default=1
    set boot_counter=-1
  # otherwise decrement boot_counter
  else
    decrement boot_counter
  fi
  save_env boot_counter
  search --no-floppy --fs-uuid --set=root --hint='hd0,msdos1'  b1340a90-fcf0-47ee-8498-dd010f34caf4
  search --no-floppy --fs-uuid --set=root b1340a90-fcf0-47ee-8498-dd010f34caf4
  search --no-floppy --fs-uuid --set=boot --hint='hd0,msdos1'  b1340a90-fcf0-47ee-8498-dd010f34caf4
  search --no-floppy --fs-uuid --set=boot b1340a90-fcf0-47ee-8498-dd010f34caf4
  set kernelopts="root=/dev/mapper/cs-root ro crashkernel=auto resume=/dev/mapper/cs-swap rd.lvm.lv=cs/root rd.lvm.lv=cs/swap net.ifnames=0 biosdevname=0 rhgb quiet "
  set menu_hide_ok=1
  set menu_hide_ok=0 
  set boot_indeterminate=0
  set boot_indeterminate=2
  if [ "${menu_show_once}" ]; then
    unset menu_show_once
    save_env menu_show_once
    set timeout_style=menu
    set timeout=60
  elif [ "${menu_auto_hide}" -a "${menu_hide_ok}" = "1" ]; then
    set orig_timeout_style=${timeout_style}
    set orig_timeout=${timeout}
    if [ "${fastboot}" = "1" ]; then
      # timeout_style=menu + timeout=0 avoids the countdown code keypress check
      set timeout_style=menu
      set timeout=0
    else
      set timeout_style=hidden
      set timeout=1
    fi
  fi
  source ${config_directory}/custom.cfg
  source $prefix/custom.cfg;

6.找出netstat -tan命令结果中以LISTEN后跟任意多个空白字符结尾的行

[root@server ~]# netstat -tan | grep 'LISTEN[[:space:]]*$'
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:10051           0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp6       0      0 ::1:25                  :::*                    LISTEN     
tcp6       0      0 :::10050                :::*                    LISTEN     
tcp6       0      0 :::3306                 :::*                    LISTEN     
tcp6       0      0 :::111                  :::*                    LISTEN     
tcp6       0      0 :::80                   :::*                    LISTEN     
tcp6       0      0 :::22                   :::*                    LISTEN     

7.显示centos8上所有UID小于1000以内的用户名和UID

[root@server ~]# cat /etc/passwd |awk -F ':' '{print$1,$3}'|grep '\<[0-9]\{1,3\}\>'
root 0
bin 1
daemon 2
adm 3
lp 4
sync 5
shutdown 6
halt 7
mail 8
operator 11
games 12
ftp 14
dbus 81
systemd-coredump 999
systemd-resolve 193
tss 59
polkitd 998
unbound 997
sssd 996
sshd 74
rngd 995
apache 48
nginx 994
mysql 27
zabbix 993
postfix 89
rpc 32
rpcuser 992
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值