1. 显示/etc/passwd文件中以bash结尾的行
1 | [root@www ~]# grep -n '\(bash\)$' /etc/passwd |
2. 显示/etc/passwd文件中的两位数或三位数
1 | [root@www ~]# grep -n '[[:digit:]]\{2,3\}' /etc/passwd |
3. 显示‘netstat -tan’命令结果中以‘LISTEN’后跟0个、1个或多个空白字符结尾的行
1 2345678910111213141516 | [root@www ~]# netstat -tan | grep -n "LISTEN[[:space:]]*$"3:tcp 0 0 127.0.0.1:6013 0.0.0.0:* LISTEN 4:tcp 0 0 0.0.0.0:2222 0.0.0.0:* LISTEN 5:tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 6:tcp 0 0 0.0.0.0:41811 0.0.0.0:* LISTEN 7:tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 8:tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 9:tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 10:tcp 0 0 127.0.0.1:6012 0.0.0.0:* LISTEN 12:tcp 0 0 ::1:6013 :::* LISTEN 13:tcp 0 0 :::2222 :::* LISTEN 14:tcp 0 0 :::111 :::* LISTEN 15:tcp 0 0 ::1:631 :::* LISTEN 16:tcp 0 0 ::1:25 :::* LISTEN 17:tcp 0 0 :::42138 :::* LISTEN 18:tcp 0 0 ::1:6012 :::* LISTEN |
4. 添加用户bash, testbash, basher以及nologin用户(nologin用户的shell为/sbin/nologin),而后找出/etc/passwd文件中用户名同shell名的行
1 234 | [root@www ~]# useradd bash[root@www ~]# useradd testbash[root@www ~]# useradd basher[root@www ~]# useradd nologin -s /sbin/nologin |
1 23456 | [root@www ~]# grep -n '^\(\b[[:alnum:]]\{1,\}\b\):.*\1$' /etc/passwd6:sync:x:5:0:sync:/sbin:/bin/sync7:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown8:halt:x:7:0:halt:/sbin:/sbin/halt39:bash:x:501:501::/home/bash:/bin/bash42:nologin:x:504:504::/home/nologin:/sbin/nologin |
5. 显示当前系统上root、centos或user1用户的默认的shell和UID
1 234 | [root@www ~]# egrep '^\b(root|centos|user1)\b' /etc/passwd |cut -d: -f3,70:/bin/bash 503:/bin/bash504:/bin/bash |
6. 找出/etc/rc.d/init.d/functions文件中某单词(单词中间可以存在下划线)后面跟着一组小括号的行
1 234567891011121314151617181920212223242526272829303132333435 | [root@www ~]# egrep -n --color '^(\b(\w{1,})\b)\(\)' /etc/rc.d/init.d/functions54:fstab_decode_str() { 59:checkpid() { 68:__readlink() { 72:__fgrep() { 87:__umount_loop() { 115:__umount_loopback_loop() { 148:__pids_var_run() { 184:__pids_pidof() { 191:daemon() { 281:killproc() { 382:pidfileofproc() { 397:pidofproc() { 423:status() { 487:echo_success() { 498:echo_failure() { 509:echo_passed() { 520:echo_warning() { 532:update_boot_stage() { 540:success() { 546:failure() { 554:passed() { 561:warning() { 568:action() { 581:action_silent() { 594:strstr() { 600:confirm() { 618:get_numeric_dev() { 629:is_ignored_file() { 639:is_true() { 649:is_false() { 659:apply_sysctl() { 667:key_is_random() { 672:find_crypto_mount_point() { 685:init_crypto() { |
7. 使用echo输出一个路径,而后egrep找出其路径基名,进一步地使用egrep取出其目录名
1 23456 | [root@www ~]# echo /etc/sysconfig/network/ | egrep -o --color '[[:alpha:]]+/?$' |cut -d/ -f1network[root@www ~]# echo /etc/sysconfig/network/ | egrep -o --color '^(/)\b.*\1\b' /etc/sysconfig/[root@www ~]# echo /etc/sysconfig/network/ | egrep -o --color '^(/)\b.*\1\b' |cut -d/ -f1-3/etc/sysconfig |
8. 找出ifconfig命令执行结果中1-255之间的数字
1 234567891011121314 | [root@www ~]# ifconfig | egrep --color '\b([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\b'eth0 Link encap:Ethernet HWaddr 00:0C:29:A6:0E:39 inet addr:172.16.10.101 Bcast:172.16.10.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fea6:e39/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX bytes:2573958 (2.4 MiB) TX bytes:2567184 (2.4 MiB) inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:12 errors:0 dropped:0 overruns:0 frame:0 TX packets:12 errors:0 dropped:0 overruns:0 carrier:0virbr0 Link encap:Ethernet HWaddr 52:54:00:6F:2D:C1 inet addr:192.168.122.1 Bcast:192.168.122.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 |