码迷,mamicode.com
首页 > 其他好文 > 详细

08-27作业

时间:2015-09-03 07:01:15      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:编辑器   生成器   表达式   linux   color   

Linux 文本处理三大工具:

        grep: 文本过滤工具;

                   sed:文本编辑器()                  

awk:文本报告生成器;

gerp

使用格式:grep [OPTIONS] PATTERN [FILE...]

选项:

--color=auto:对匹配到的串做高亮显示;(CentOS7默认带有,CentOS6需要手动填写)

                            -v:显示模式匹配不到行;

                            -i:忽略字符大小写;

                            -o:仅显示能够被模式匹配到的串本行;

                            -q:静默模式;

                            -E:使用扩展的正则表达式;

元字符:

字符匹配:

                                     .:匹配任意单个字符;

                                     []:匹配指定范围内的任意单个字符;

                                     [^]:匹配指定范围外的任意单个字符;

                      次数匹配:用于要指定其次数的字符的后面;

                                     *:任意次;

                                              

                                     \?01次;

                                     \+1或多次;

                                     \{m\}:精确限制为m次;

                                     \{m,n\}:至少m次,至多n次,[m,n]

                                               \{0,n\}:至多n次;

                                               \{m,\}:至少m次;

                                     .*:匹配任意长度的任意字符;

                            位置锚定:

                                     ^:行首锚定;用于模式的最左侧;

                                     $:行尾锚定;用于模式的最右侧;

                                     \<,\b: 词首锚定;用于表示单词的模式的左侧;

                                     \>,\b:词尾锚定;用于表示单词的模式的右侧;

                                     ^$:空白行;

 

                            分组:\(\)

 

                                     分组的小括号中的模式匹配到的内容,会在执行过程中被正则表达式引擎记录下来,并保存内置的变量中;这些变量分别是\1, \2, ...

Egrep

 grep 用法基本相同,区别在于次数匹配时不需要再转义!

练习题

显示/etc/passwd文件中以bash结尾的行

[root@localhost ~]# grep bash$ /etc/passwd

root:x:0:0:root:/root:/bin/bash

lishuai:x:500:500:davachi:/home/lishuai:/bin/bash

[root@localhost ~]#

显示/etc/passwd文件中的两位数或三位数

[root@localhost ~]# grep "\<[0-9]\{2,3\}\>"/etc/passwd

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

operator:x:11:0:operator:/root:/sbin/nologin

games:x:12:100:games:/usr/games:/sbin/nologin

gopher:x:13:30:gopher:/var/gopher:/sbin/nologin

ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

nobody:x:99:99:Nobody:/:/sbin/nologin

dbus:x:81:81:System messagebus:/:/sbin/nologin

usbmuxd:x:113:113:usbmuxduser:/:/sbin/nologin

vcsa:x:69:69:virtual console memoryowner:/dev:/sbin/nologin

rpc:x:32:32:RpcbindDaemon:/var/cache/rpcbind:/sbin/nologin

rtkit:x:499:497:RealtimeKit:/proc:/sbin/nologin

avahi-autoipd:x:170:170:Avahi IPv4LLStack:/var/lib/avahi-autoipd:/sbin/nologin

abrt:x:173:173::/etc/abrt:/sbin/nologin

rpcuser:x:29:29:RPC ServiceUser:/var/lib/nfs:/sbin/nologin

haldaemon:x:68:68:HALdaemon:/:/sbin/nologin

gdm:x:42:42::/var/lib/gdm:/sbin/nologin

ntp:x:38:38::/etc/ntp:/sbin/nologin

apache:x:48:48:Apache:/var/www:/sbin/nologin

saslauth:x:498:76:Saslauthduser:/var/empty/saslauth:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

pulse:x:497:496:PulseAudio SystemDaemon:/var/run/pulse:/sbin/nologin

sshd:x:74:74:Privilege-separatedSSH:/var/empty/sshd:/sbin/nologin

tcpdump:x:72:72::/:/sbin/nologin

lishuai:x:500:500:davachi:/home/lishuai:/bin/bash

显示`netstat -tan`命令结果中以‘LISTEN’后跟0个、1个或者多个空白字符结尾的行

[root@localhost ~]# netstat -tan | grep"\<LISTEN\> [[:space:]]\{0,\}$"

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     

tcp       0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN     

tcp       0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN     

tcp       0      0 0.0.0.0:44060               0.0.0.0:*                   LISTEN     

tcp       0      0 :::49155                    :::*                        LISTEN     

tcp       0      0 :::111                      :::*                        LISTEN     

tcp       0      0 :::22                       :::*                        LISTEN     

tcp       0      0 ::1:631                     :::*                        LISTEN     

tcp       0      0 ::1:25                      :::*                        LISTEN     

添加用户bashtestbashbasher以及nologin用户(nologin用户的shell/sbin/nologin);

而后找出/etc/passwd文件中用户名与其shell名相同的行

[root@localhost ~]# useradd bash

[root@localhost ~]# useradd textbash

[root@localhost ~]# useradd basher

[root@localhost ~]# useradd -s/sbin/nologin nologin

[root@localhost ~]# egrep    ‘(\b[a-z]+[a-z]\b).*\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:501:3002::/home/bash:/bin/bash

nologin:x:504:504::/home/nologin:/sbin/nologin

[root@localhost ~]#

显示当前系统上rootcentos或者user1用户的默认shellUID (请事先创建这些用户,若不存在)

[root@localhost ~]# egrep‘^\<root|centos|user1\>‘ /etc/passwd

root:x:0:0:root:/root:/bin/bash

centos:x:505:505::/home/centos:/bin/bash

user1:x:506:506::/home/user1:/bin/bash

找出/etc/rc.d/init.d/functions文件中某单词(单词中间可以存在下划线)后面跟着一组小括号的行

[root@localhost ~]# egrep ‘\<[[:alpha:]_]+\(\)‘/etc/rc.d/init.d/functions

fstab_decode_str() {

checkpid() {

__readlink() {

__fgrep() {

__umount_loop() {

__umount_loopback_loop() {

__pids_var_run() {

__pids_pidof() {

daemon() {

killproc() {

pidfileofproc() {

pidofproc() {

status() {

echo_success() {

echo_failure() {

echo_passed() {

echo_warning() {

update_boot_stage() {

success() {

failure() {

passed() {

warning() {

action() {

action_silent() {

strstr() {

confirm() {

get_numeric_dev() {

is_ignored_file() {

is_true() {

is_false() {

apply_sysctl() {

key_is_random() {

find_crypto_mount_point() {

init_crypto() {

[root@localhost ~]#

使用echo输出一个路径,而后egrep找出其路径基名;进一步的使用egrep取出其目录名

路径基名

[root@localhost 12]# echo /1/12/123 | egrep -o ‘([^/]*)$‘

123

[root@localhost 12]#

找出ifconfig命令执行结果中1-255之间的数字

[root@localhost ~]# ifconfig | egrep -n‘\b[1-9]\b|\b[1-9][0-9]\b|\b[1-2][0-9][0-5]\b|\b[1][0-9][0-9]\b‘

1:eth0      Linkencap:Ethernet  HWaddr00:0C:29:F9:D4:CE 

2:          inetaddr:192.168.200.128 Bcast:192.168.200.255 Mask:255.255.255.0

3:          inet6 addr:fe80::20c:29ff:fef9:d4ce/64 Scope:Link

4:          UP BROADCASTRUNNING MULTICAST  MTU:1500  Metric:1

8:          RXbytes:23884192 (22.7 MiB)  TXbytes:1152869 (1.0 MiB)

11:          inetaddr:127.0.0.1  Mask:255.0.0.0

12:          inet6 addr:::1/128 Scope:Host

13:          UP LOOPBACKRUNNING  MTU:65536  Metric:1

14:          RXpackets:224 errors:0 dropped:0 overruns:0 frame:0

15:          TXpackets:224 errors:0 dropped:0 overruns:0 carrier:0

17:          RXbytes:17354 (16.9 KiB)  TX bytes:17354(16.9 KiB)

08-27作业

标签:编辑器   生成器   表达式   linux   color   

原文地址:http://pioneerls.blog.51cto.com/5835787/1691005

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!