zoukankan      html  css  js  c++  java
  • 三剑客

    三剑客(四个文本处理工具)
    Awk、Sed、Grep、Find。
    
    
    
    Grep  文本过滤,会给过滤出来的内容加上颜色。
    
    centos6需要自己设置别名,centos7不需要
    
    选项:
    	-n		#给过滤出来的内容加上当前内容所在的行号。
    	-v		#取反,排除
    	-E		#支持扩展正则使用
    	-i		#忽略大小写
    	-o		#只显示匹配的内容
    	-w		#只匹配关键字(一整串的字符串)
    	-c 		#统计过滤出来的内容行数
    	-A 		#显示匹配内容的向下的行数
    	-B		#向上匹配
    	-C		#上下匹配
    	-r		#递归过滤
    	-R		#递归过滤 
    	
    

    -n选项

    [root@oldboyedu ~]# grep -n 'root'  passwd 
    1:root:x:0:0:root:/root:/bin/bash
    10:operator:x:11:0:operator:/root:/sbin/nologin
    

    -i选项

    [root@oldboyedu ~]# echo  Root  >> passwd
    [root@oldboyedu ~]# grep 'root'  passwd
    root:x:0:0:root:/root:/bin/bash
    operator:x:11:0:operator:/root:/sbin/nologin
    [root@oldboyedu ~]# grep -i 'root' passwd
    root:x:0:0:root:/root:/bin/bash
    operator:x:11:0:operator:/root:/sbin/nologin
    Root
    

    -v选项

    [root@oldboyedu ~]# grep -v  '/sbin/nologin' passwd
    root:x:0:0:root:/root:/bin/bash
    sync:x:5:0:sync:/sbin:/bin/sync
    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    halt:x:7:0:halt:/sbin:/sbin/halt
    Root
    

    -w

    [root@oldboyedu ~]# echo roott >>passwd
    [root@oldboyedu ~]# grep  'root'  passwd |wc -l
    3
    [root@oldboyedu ~]# grep  'root'  passwd 
    root:x:0:0:root:/root:/bin/bash
    operator:x:11:0:operator:/root:/sbin/nologin
    roott
    [root@oldboyedu ~]# grep -w 'root'  passwd
    root:x:0:0:root:/root:/bin/bash
    operator:x:11:0:operator:/root:/sbin/nologin
    

    -c选项

    [root@oldboyedu ~]# grep -c 'root' passwd 
    3
    [root@oldboyedu ~]# grep -c 'root' passwd |wc -l
    1
    [root@oldboyedu ~]# grep  'root' passwd |wc -l
    3
    [root@oldboyedu ~]# grep -c '.' passwd 
    38
    [root@oldboyedu ~]# wc -l passwd
    38 passwd
    	
    [root@oldboyedu ~]# grep -R 'root'  ./
    ./.bash_history:cd /root/nginx-1.16.0
    ./.bash_history:fpm -s dir -t rpm -v 1.16.0 -n nginx -d 'pcre-devel,openssl-devel,zlib-devel' --post-install /root/nginx.sh -f /app/nginx.1.16.0/
    ./.bash_history:fpm -s dir -t rpm -v 1.16.0 -n nginx -d 'pcre-devel,openssl-devel,zlib-devel' --post-install /root/nginx.sh -f /app/nginx.1.16.0/
    ./.bash_history:mv /root/nginx-1.16.0-1.x86_64.rpm  .
    ./.bash_history:top -d 1 -u root
    ./.bash_history:pgrep -u root
    ./.bash_history:pgrep -u -l root
    ./.bash_history:pgrep -l -u root
    ./.bash_history:top -d 1 -u root
    ./.bash_history:wget [root@oldboyedu ~]# kill 9461
    ./.bash_history:mkpasswd -l 20 |passwd --stdin root
    Binary file ./.gem/specs/mirrors.huaweicloud.com%443/repository/rubygems/specs.4.8 matches
    Binary file ./.gem/specs/mirrors.huaweicloud.com%443/repository/rubygems/latest_specs.4.8 matches
    Binary file ./.file2.swp matches
    Binary file ./sysstat-11.7.3-1.x86_64.rpm matches
    ./1111/test.txt:root:x:0:0:root:/root:/bin/bash
    ./1111/test.txt:operator:x:11:0:operator:/root:/sbin/nologin
    ./1111/test.txt:roott
    ./passwd:root:x:0:0:root:/root:/bin/bash
    ./passwd:operator:x:11:0:operator:/root:/sbin/nologin
    ./passwd:roott
    [root@oldboyedu ~]# grep -r 'root'  ./
    ./.bash_history:cd /root/nginx-1.16.0
    ./.bash_history:fpm -s dir -t rpm -v 1.16.0 -n nginx -d 'pcre-devel,openssl-devel,zlib-devel' --post-install /root/nginx.sh -f /app/nginx.1.16.0/
    ./.bash_history:fpm -s dir -t rpm -v 1.16.0 -n nginx -d 'pcre-devel,openssl-devel,zlib-devel' --post-install /root/nginx.sh -f /app/nginx.1.16.0/
    ./.bash_history:mv /root/nginx-1.16.0-1.x86_64.rpm  .
    ./.bash_history:top -d 1 -u root
    ./.bash_history:pgrep -u root
    ./.bash_history:pgrep -u -l root
    ./.bash_history:pgrep -l -u root
    ./.bash_history:top -d 1 -u root
    ./.bash_history:wget [root@oldboyedu ~]# kill 9461
    ./.bash_history:mkpasswd -l 20 |passwd --stdin root
    Binary file ./.gem/specs/mirrors.huaweicloud.com%443/repository/rubygems/specs.4.8 matches
    Binary file ./.gem/specs/mirrors.huaweicloud.com%443/repository/rubygems/latest_specs.4.8 matches
    Binary file ./.file2.swp matches
    Binary file ./sysstat-11.7.3-1.x86_64.rpm matches
    ./1111/test.txt:root:x:0:0:root:/root:/bin/bash
    ./1111/test.txt:operator:x:11:0:operator:/root:/sbin/nologin
    ./1111/test.txt:roott
    ./passwd:root:x:0:0:root:/root:/bin/bash
    ./passwd:operator:x:11:0:operator:/root:/sbin/nologin
    ./passwd:roott
    	
    

    -A

    [root@oldboyedu ~]# grep -n '.' passwd 
    1:root:x:0:0:root:/root:/bin/bash
    2:bin:x:1:1:bin:/bin:/sbin/nologin
    3:daemon:x:2:2:daemon:/sbin:/sbin/nologin
    4:adm:x:3:4:adm:/var/adm:/sbin/nologin
    5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    6:sync:x:5:0:sync:/sbin:/bin/sync
    7:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    8:halt:x:7:0:halt:/sbin:/sbin/halt
    9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    10:operator:x:11:0:operator:/root:/sbin/nologin
    11:games:x:12:100:games:/usr/games:/sbin/nologin
    12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    13:nobody:x:99:99:Nobody:/:/sbin/nologin
    14:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
    15:dbus:x:81:81:System message bus:/:/sbin/nologin
    16:polkitd:x:999:998:User for polkitd:/:/sbin/nologin
    17:tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
    18:abrt:x:173:173::/etc/abrt:/sbin/nologin
    19:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    20:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    21:nginx:x:998:996:Nginx web server:/var/lib/nginx:/sbin/nologin
    22:redis:x:997:995:Redis Database Server:/var/lib/redis:/sbin/nologin
    23:ntp:x:38:38::/etc/ntp:/sbin/nologin
    24:rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
    25:rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
    26:nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
    27:oldgirl1:x:1010:1010::/home/oldgirl1:/sbin/nologin
    28:oldgirl2:x:1011:1011::/home/oldgirl2:/sbin/nologin
    29:oldgirl3:x:1012:1012::/home/oldgirl3:/sbin/nologin
    30:oldgirl4:x:1013:1013::/home/oldgirl4:/sbin/nologin
    31:oldgirl5:x:1014:1014::/home/oldgirl5:/sbin/nologin
    32:oldgirl6:x:1015:1015::/home/oldgirl6:/sbin/nologin
    33:oldgirl7:x:1016:1016::/home/oldgirl7:/sbin/nologin
    34:oldgirl8:x:1017:1017::/home/oldgirl8:/sbin/nologin
    35:oldgirl9:x:1018:1018::/home/oldgirl9:/sbin/nologin
    36:oldgirl10:x:1019:1019::/home/oldgirl10:/sbin/nologin
    37:Root
    38:roott
    [root@oldboyedu ~]# grep -n '.' passwd |grep  -w '11'
    10:operator:x:11:0:operator:/root:/sbin/nologin
    11:games:x:12:100:games:/usr/games:/sbin/nologin
    [root@oldboyedu ~]# grep -n '.' passwd |grep  -w '^11'
    11:games:x:12:100:games:/usr/games:/sbin/nologin
    [root@oldboyedu ~]# grep -n '.' passwd |grep  -wA9  '^11'
    11:games:x:12:100:games:/usr/games:/sbin/nologin
    12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    13:nobody:x:99:99:Nobody:/:/sbin/nologin
    14:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
    15:dbus:x:81:81:System message bus:/:/sbin/nologin
    16:polkitd:x:999:998:User for polkitd:/:/sbin/nologin
    17:tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
    18:abrt:x:173:173::/etc/abrt:/sbin/nologin
    19:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    20:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    	
    	
    

    -B选项

    [root@oldboyedu ~]# grep -n '.' passwd  |grep -w '^20'
    20:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    [root@oldboyedu ~]# grep -n '.' passwd  |grep -wB 9  '^20' 
    11:games:x:12:100:games:/usr/games:/sbin/nologin
    12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    13:nobody:x:99:99:Nobody:/:/sbin/nologin
    14:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
    15:dbus:x:81:81:System message bus:/:/sbin/nologin
    16:polkitd:x:999:998:User for polkitd:/:/sbin/nologin
    17:tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
    18:abrt:x:173:173::/etc/abrt:/sbin/nologin
    19:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    20:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    


    -C选项

    ##### [root@oldboyedu ~]# grep  -n '.' passwd  |grep -Ew '^15|16'
    
    15:dbus:x:81:81:System message bus:/:/sbin/nologin
    16:polkitd:x:999:998:User for polkitd:/:/sbin/nologin
    [root@oldboyedu ~]# grep  -n '.' passwd  |grep -Ew '^15|^16'
    15:dbus:x:81:81:System message bus:/:/sbin/nologin
    16:polkitd:x:999:998:User for polkitd:/:/sbin/nologin
    [root@oldboyedu ~]# grep  -n '.' passwd  |grep -EwC4  '^15|^16'
    11:games:x:12:100:games:/usr/games:/sbin/nologin
    12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    13:nobody:x:99:99:Nobody:/:/sbin/nologin
    14:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
    15:dbus:x:81:81:System message bus:/:/sbin/nologin
    16:polkitd:x:999:998:User for polkitd:/:/sbin/nologin
    17:tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
    18:abrt:x:173:173::/etc/abrt:/sbin/nologin
    19:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    20:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    [root@oldboyedu ~]# grep  -n '.' passwd  |grep -EwC4  '^1[56]'
    11:games:x:12:100:games:/usr/games:/sbin/nologin
    12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    13:nobody:x:99:99:Nobody:/:/sbin/nologin
    14:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
    15:dbus:x:81:81:System message bus:/:/sbin/nologin
    16:polkitd:x:999:998:User for polkitd:/:/sbin/nologin
    17:tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
    18:abrt:x:173:173::/etc/abrt:/sbin/nologin
    19:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    20:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    [root@oldboyedu ~]# grep  -n '.' /etc/passwd  |grep -EwC4  '^1[56]'
    11:games:x:12:100:games:/usr/games:/sbin/nologin
    12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    13:nobody:x:99:99:Nobody:/:/sbin/nologin
    14:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
    15:dbus:x:81:81:System message bus:/:/sbin/nologin
    16:polkitd:x:999:998:User for polkitd:/:/sbin/nologin
    17:tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
    18:abrt:x:173:173::/etc/abrt:/sbin/nologin
    19:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    20:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    

    正则:

    ^

    [root@oldboyedu ~]# grep '^root' passwd 
    root:x:0:0:root:/root:/bin/bash
    roott
    [root@oldboyedu ~]# grep '^r' passwd 
    root:x:0:0:root:/root:/bin/bash
    redis:x:997:995:Redis Database Server:/var/lib/redis:/sbin/nologin
    rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
    rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
    roott
    

    $

    [root@oldboyedu ~]# grep  'h$' passwd 
    root:x:0:0:root:/root:/bin/bash
    [root@oldboyedu ~]# grep  'bash$' passwd 
    root:x:0:0:root:/root:/bin/bash
    

    空行

    [root@oldboyedu ~]# grep -v '^$' passwd
    #里面空格和tab键
    [root@oldboyedu ~]# grep -v '^s*$' passwd
    
    [root@oldboyedu ~]# grep -n '^s*$' passwd 
    
    #.点匹配任意一个字符
    [root@oldboyedu ~]# grep '.' test.log 
    123
    [root@oldboyedu ~]# grep -o '.' test.log 
    1
    2
    3
    
    #环境准备
    cat>oldboy.txt<<EOF
    I am oldboy teacher!
    I teach linux.
    I like badminton ball ,billiard ball and chinese chess!
    my blog is http://oldboy.blog.51cto.com
    our size is http://blog.oldboyedu.com
    my qq is 49000448
    not 4900000448.
    my god ,i am not oldbey,but OLDBOY!
    EOF
    

    转义,取消特殊字符的特殊含义。

    #只过滤以点为结尾的行
    [root@oldboyedu ~]# grep  '.$'  oldboy.txt 
    I am oldboy teacher!
    I teach linux.
    I like badminton ball ,billiard ball and chinese chess!
    my blog is http://oldboy.blog.51cto.com
    our size is http://blog.oldboyedu.com
    my qq is 49000448
    not 4900000448.
    my god ,i am not oldbey,but OLDBOY!
    [root@oldboyedu ~]# grep  '.$'  oldboy.txt 
    I teach linux.
    not 4900000448.
    
    #以m或者n开头的行
    

    [] 表示多个字符串,会单个匹配[]里面的内容,[] [^] 的区别

    | 或者

    [root@oldboyedu ~]# grep '^[mn]'  oldboy.txt
    my blog is http://oldboy.blog.51cto.com
    my qq is 49000448
    not 4900000448.
    my god ,i am not oldbey,but OLDBOY
    [root@oldboyedu ~]# grep '^m|^n' oldboy.txt -E
    my blog is http://oldboy.blog.51cto.com
    my qq is 49000448
    not 4900000448.
    my god ,i am not oldbey,but OLDBOY!
    
    [root@oldboyedu ~]# grep -v '^[mn]'  oldboy.txt
    I am oldboy teacher!
    I teach linux.
    I like badminton ball ,billiard ball and chinese chess!
    our size is http://blog.oldboyedu.com
    [root@oldboyedu ~]# grep '^[^mn]' oldboy.txt
    I am oldboy teacher!
    I teach linux.
    I like badminton ball ,billiard ball and chinese chess!
    our size is http://blog.oldboyedu.com
    [root@oldboyedu ~]# grep '[^mn]' oldboy.txt
    I am oldboy teacher!
    I teach linux.
    I like badminton ball ,billiard ball and chinese chess!
    my blog is http://oldboy.blog.51cto.com
    our size is http://blog.oldboyedu.com
    my qq is 49000448
    not 4900000448.
    my god ,i am not oldbey,but OLDBOY!
    

    *前面一个字符出现0次或0次以上

    [root@oldboyedu ~]# grep -o '0' oldboy.txt 
    0
    0
    0
    0
    0
    0
    0
    0
    [root@oldboyedu ~]# grep -o '0*' oldboy.txt 
    000
    00000
    

    .* 表示所有的意思

    [root@oldboyedu ~]# grep -o  '.*' oldboy.txt 
    I am oldboy teacher!
    I teach linux.
    I like badminton ball ,billiard ball and chinese chess!
    my blog is http://oldboy.blog.51cto.com
    our size is http://blog.oldboyedu.com
    my qq is 49000448
    not 4900000448.
    my god ,i am not oldbey,but OLDBOY!
    
    cat>id.txt<<EOF
    汤 610702199107053598
    邹 371481199403259478
    莫 52020319810613433X
    韩 460106199111137270
    荣 530124197504135438
    荣 oldboy
    荣 babygirl
    荣 530124197504135438
    阮 360702197902169951
    翁 331002198504132813
    任 6212231987082X5176
    姜 370602198507189574
    李A BBBCCC98507189574
    赵B BBB602198507189574
    霍 320904198403048179
    EOF
    

    {n,m} 匹配其前面的字符至少n次,至多m次

    [root@oldboyedu ~]# grep -E   '[0-9]{17}[0-9X]{1}' id.txt 
    汤 610702199107053598
    邹 371481199403259478
    莫 52020319810613433X
    韩 460106199111137270
    荣 530124197504135438
    荣 530124197504135438
    阮 360702197902169951
    翁 331002198504132813
    姜 370602198507189574
    霍 320904198403048179
    [root@oldboyedu ~]# grep -E  -o  '[0-9]{17}[0-9X]{1}' id.txt 
    610702199107053598
    371481199403259478
    52020319810613433X
    460106199111137270
    530124197504135438
    530124197504135438
    360702197902169951
    331002198504132813
    370602198507189574
    320904198403048179
    

    清理nginx配置文件

    [root@oldboyedu ~]# grep -Ev  '^s*$|#' /etc/nginx/nginx.conf.default  >nginx.conf
    [root@oldboyedu ~]# cat nginx.conf
    worker_processes  1;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
        server {
            listen       80;
            server_name  localhost;
            location / {
                root   html;
                index  index.html index.htm;
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
    }
    
    #查看配置文件
    [root@oldboyedu ~]# grep '^[a-Z]'  /etc/ssh/sshd_config
    HostKey /etc/ssh/ssh_host_rsa_key
    HostKey /etc/ssh/ssh_host_ecdsa_key
    HostKey /etc/ssh/ssh_host_ed25519_key
    SyslogFacility AUTHPRIV
    AuthorizedKeysFile	.ssh/authorized_keys
    PasswordAuthentication yes
    ChallengeResponseAuthentication no
    GSSAPIAuthentication no
    GSSAPICleanupCredentials no
    UsePAM yes
    X11Forwarding yes
    UseDNS no
    AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
    AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
    AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
    AcceptEnv XMODIFIERS
    Subsystem	sftp	/usr/libexec/openssh/sftp-server
    
    [root@oldboyedu ~]# grep -Ev '^$|^#' /etc/ssh/sshd_config
    
    Sed 替换,取行。增删改查
    选项:
    		-n 		#取消默认输出,p
    		-i 		#替换 
    		-i.bak	#先备份,在替换
    		-r 		#支持扩展正则使用 
    		s		#替换 
    		g 		#全局
    		d 		#删除
    		p 	 	#打印
    		!		#取反 
    		a 		#追加
    		i 		#插入
    		$		#结尾的行
    
    #取行。
    #取单行
    [root@oldboyedu ~]# sed  -n  '3p' passwd 
    bin:x:1:1:bin:/bin:/sbin/nologin
    #取连续的多行
    [root@oldboyedu ~]# sed -n '5,10p' 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
    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
    
    #取不连续的多行	 
    [root@oldboyedu ~]# sed -n '5p;10p' passwd
    sync:x:5:0:sync:/sbin:/bin/sync
    games:x:12:100:games:/usr/games:/sbin/nologin
    	
    

    取反,删除。

    删除单行

    [root@oldboyedu ~]# sed  '2d'  passwd 
    

    删除连续的多行

    [root@oldboyedu ~]# sed '2,10d' passwd
    

    删除不连续的多行

    [root@oldboyedu ~]# sed '2d;10d' passwd
    

    排除

    p' passwd
    
    #环境准备 
    cat >sed.txt<<"EOF"
    101,$oldboy,CEO
    102,$zhangyao,CTO
    103,$Alex,COO
    104,$yy,CFO
    105,$feixue,CIO
    EOF
    
    #过滤字符串
    [root@oldboyedu ~]# sed -n  '/yy/p'  sed.txt
    104,$yy,CFO
    
    #过滤多个
    [root@oldboyedu ~]# sed -n '/oldboy/p ; /yy/p' sed.txt
    101,$oldboy,CEO
    104,$yy,CF
    [root@oldboyedu ~]# sed -nr '/oldboy|yy/p'  sed.txt
    101,$oldboy,CEO
    104,$yy,CFO
    
    [root@oldboyedu ~]# sed -n '/oldboy/p;/yy/p' sed.txt
    101,$oldboy,CEO
    104,$yy,CFO
    
    #过滤多个连续的字符串
    [root@oldboyedu ~]# sed -n '/oldboy/,/yy/p' sed.txt
    101,$oldboy,CEO
    102,$zhangyao,CTO
    103,$Alex,COO
    104,$yy,CFO
    

    a i

    #在第三行后面追加内容
    [root@oldboyedu ~]# sed '3aoldboy' sed.txt
    101,$oldboy,CEO
    102,$zhangyao,CTO
    103,$Alex,COO
    oldboy
    104,$yy,CFO
    105,$feixue,CIO
    
    #在第四行的上面插入内容
    [root@oldboyedu ~]# sed '4ioldboy'  sed.txt
    101,$oldboy,CEO
    102,$zhangyao,CTO
    103,$Alex,COO
    oldboy
    104,$yy,CFO
    105,$feixue,CIO
    
    #插入多行
    [root@oldboyedu ~]# sed '4ioldboy
    oldgirl'  sed.txt
    101,$oldboy,CEO
    102,$zhangyao,CTO
    103,$Alex,COO
    oldboy
    oldgirl
    104,$yy,CFO
    105,$feixue,CIO
    
    #追加多行
    [root@oldboyedu ~]# sed '3aoldboy
    oldgirl'  sed.txt
    101,$oldboy,CEO
    102,$zhangyao,CTO
    103,$Alex,COO
    oldboy
    oldgirl
    104,$yy,CFO
    105,$feixue,CIO
    
    #在结尾追加内容
    [root@oldboyedu ~]# sed  '$aoldboy'  sed.txt
    101,$oldboy,CEO
    102,$zhangyao,CTO
    103,$Alex,COO
    104,$yy,CFO
    105,$feixue,CIO
    oldboy
    

    #全局
    [root@oldboyedu ~]# sed 's#oldboy#jinke#g'  sed.txt
    101,$jinke,CEO
    102,$zhangyao,CTO
    103,$Alex,COO
    104,$yy,CFO
    105,$feixue,CIO
    [root@oldboyedu ~]# sed -i.org  's#oldboy#jinke#g'  sed.txt
    
    [root@oldboyedu ~]# sed 's#^SELINUX=.*#SELINUX=disabled#g'  /etc/sysconfig/selinux
    
    #c  原位置替换
    [root@oldboyedu ~]# sed  '/^SELINUX=/c SELINUX=enforcing' /etc/sysconfig/selinux
    
    #第一行到第五行的root替换 
    [root@oldboyedu ~]# sed '1,5s#root#oldboy#g' passwd
    
    #替换第一行的root
    [root@oldboyedu ~]# sed '1s#root#oldboy#g' passwd
    
    #替换特殊字符使用转义
    [root@oldboyedu ~]# sed 's#$#%#g' sed.txt
    101,$jinke,CEO%
    102,$zhangyao,CTO%
    103,$Alex,COO%
    104,$yy,CFO%
    105,$feixue,CIO%
    [root@oldboyedu ~]# sed 's#$#%#g' sed.txt
    101,%jinke,CEO
    102,%zhangyao,CTO
    103,%Alex,COO
    104,%yy,CFO
    105,%feixue,CIO
    
    [root@oldboyedu ~]# sed 's//sbin/nologin/oldboy/g' passwd
    
    #后向引用
    [root@oldboyedu ~]# ip a s eth0
    2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
        link/ether 00:0c:29:1d:ca:3a brd ff:ff:ff:ff:ff:ff
        inet 10.0.0.150/24 brd 10.0.0.255 scope global noprefixroute eth0
           valid_lft forever preferred_lft forever
        inet6 fe80::80b2:dc89:7adf:c7b1/64 scope link noprefixroute 
           valid_lft forever preferred_lft forever
    [root@oldboyedu ~]# ip a s eth0 | sed -n '3p'
        inet 10.0.0.150/24 brd 10.0.0.255 scope global noprefixroute eth0
    [root@oldboyedu ~]# ip a s eth0 | sed -n '3p' |sed -r  's#.*t (.*)/.*#1#g'
    10.0.0.150
    [root@oldboyedu ~]# ip a s eth0 |sed -nr  '3s#.*t (.*)/.*#1#gp'
    10.0.0.150
    
    [root@oldboyedu ~]# ifconfig  eth0
    eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 10.0.0.150  netmask 255.255.255.0  broadcast 10.0.0.255
            inet6 fe80::80b2:dc89:7adf:c7b1  prefixlen 64  scopeid 0x20<link>
            ether 00:0c:29:1d:ca:3a  txqueuelen 1000  (Ethernet)
            RX packets 20849  bytes 1902050 (1.8 MiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 15800  bytes 2443358 (2.3 MiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    [root@oldboyedu ~]# ifconfig  eth0 |sed -n  '2p'
            inet 10.0.0.150  netmask 255.255.255.0  broadcast 10.0.0.255
    [root@oldboyedu ~]# ifconfig  eth0 |sed -nr  '2s#.*t (.*)#1#gp'
    10.0.0.255
    [root@oldboyedu ~]# ifconfig  eth0 |sed -nr  '2s#.*t (.*)  n.*#1#gp'
    10.0.0.150
    
    [root@oldboyedu ~]# stat sed.txt
      File: ‘sed.txt’
      Size: 75        	Blocks: 8          IO Block: 4096   regular file
    Device: 803h/2051d	Inode: 67160646    Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2019-07-25 11:34:50.285127299 +0800
    Modify: 2019-07-25 11:34:29.851127028 +0800
    Change: 2019-07-25 11:34:29.851127028 +0800
     Birth: -
    
     
    
    cat>/root/hostname_ip.sh<<"EOF"
    #!/usr/bin/sh
    source /etc/init.d/functions
    if [ $# -ne 2 ];then
      echo "/bin/sh $0 hostname PartIP"
      exit 1
    fi
    hostnamectl  set-hostname   $1
    if [ $? -eq 0 ];then
        action "hostname update Successful!" /bin/true
    else
        action "hostname update Failed!" /bin/false
    fi
    Ip=$(ip a s eth0 |awk -F '[./]' 'NR==3{print $4}')
    sed -i "s#$Ip#$2#g" /etc/sysconfig/network-scripts/ifcfg-eth[01]
    if [ $? -eq 0 ];then
        action "IP update Successful!" /bin/true
    else
        action "IP update Failed!" /bin/false
    fi
    systemctl  restart  network
    bash
    EOF
    
     
    
    Awk #取列,计算,统计。数组。
    选项: 
    		-F   FS   #输入分隔符,默认是空格
    		-v		  #配合输出分隔符使用
    		OFS		  #输出分隔符
    		BEGIN    END
    		$n  	  #某列
    		NR		  #行号
    		$0		  #完整内容
    		$NF		  #最后一列
    		i++		  #计算次数
    		i=i+$N    #计算和
    

    取行

    [root@oldboy ~]# awk  'NR==3'  sed.txt
    103,$Alex,COO
    [root@oldboy ~]# awk  'NR==1,NR==3'  sed.txt
    101,$jinke,CEO
    102,$zhangyao,CTO
    103,$Alex,COO
    [root@oldboy ~]# awk 'NR==1;NR==3'  sed.txt
    101,$jinke,CEO
    103,$Alex,COO
    [root@oldboy ~]# awk  'NR>1 && NR<3'  sed.txt
    102,$zhangyao,CTO
    [root@oldboy ~]# awk  'NR>=1 && NR<=3'  sed.txt
    101,$jinke,CEO
    102,$zhangyao,CTO
    103,$Alex,COO
    

    过滤

    [root@oldboy ~]# awk  '/jinke/' sed.txt
    101,$jinke,CEO
    [root@oldboy ~]# awk  '!/jinke/' sed.txt
    102,$zhangyao,CTO
    103,$Alex,COO
    104,$yy,CFO
    105,$feixue,CIO
    [root@oldboy ~]# awk '/jinke|yy/' sed.txt
    101,$jinke,CEO
    104,$yy,CFO
    [root@oldboy ~]# awk '/jinke/;/yy/' sed.txt
    101,$jinke,CEO
    104,$yy,CFO
    [root@oldboy ~]# awk '/jinke/,/yy/' sed.txt
    101,$jinke,CEO
    102,$zhangyao,CTO
    103,$Alex,COO
    104,$yy,CFO
    

    取列

    [root@oldboy ~]# awk -F '[,]' '{print $3}' sed.txt
    CEO
    CTO
    COO
    CFO
    CIO
    
    #环境准备  
    cat >>reg.txt<<EOF
    Zhang Dandan 41117397 :250:100:175
    Zhang Xiaoyu 390320151 :155:90:201
    Meng Feixue 80042789 :250:60:50
    Wu Waiwai 70271111 :250:80:75
    Liu Bingbing 41117483 :250:100:175
    Wang Xiaoai 3515064655 :50:95:135
    Zi Gege 1986787350 :250:168:200
    Li Youjiu 918391635 :175:75:300
    Lao Nanhai 918391635 :250:100:175
    EOF
    
    #根据列进行匹配
    
    #第三列以3开头的行
    [root@oldboy ~]# awk '$3~/^3/' reg.txt
    Zhang Xiaoyu 390320151 :155:90:201
    Wang Xiaoai 3515064655 :50:95:135
    

    取反

    [root@oldboy ~]# awk '$3!~/^3/' reg.txt
    Zhang Dandan 41117397 :250:100:175
    Meng Feixue 80042789 :250:60:50
    Wu Waiwai 70271111 :250:80:75
    Liu Bingbing 41117483 :250:100:175
    Zi Gege 1986787350 :250:168:200
    Li Youjiu 918391635 :175:75:300
    Lao Nanhai 918391635 :250:100:175
    
    #第三列以1或5为结尾的行
    [root@oldboy ~]# awk '$3~/[15]$/' reg.txt
    Zhang Xiaoyu 390320151 :155:90:201
    Wu Waiwai 70271111 :250:80:75
    Wang Xiaoai 3515064655 :50:95:135
    Li Youjiu 918391635 :175:75:300
    Lao Nanhai 918391635 :250:100:175
    
    #显示姓zhang的人第二次的捐款金额
    [root@oldboy ~]# awk  -F '[ :]' '/^Zhang/{print $1,$2,$6}'  reg.txt
    Zhang Dandan 100
    Zhang Xiaoyu 90
    
    显示Xiaoyu的捐款.每个值时都有以$开头.如$520$200$135
    [root@oldboy ~]# awk  '/Xiaoyu/{print $4}' reg.txt  |tr ':'  '$'
    $155$90$201
    
    [root@oldboy ~]# awk  '/Xiaoyu/{print $4}' reg.txt  |sed 's#:#$#g'
    $155$90$201
    
    [root@oldboy ~]# awk  '/Xiaoyu/{print $4}' reg.txt  |sed 's#:#$#g'
    $155$90$201
    
    [root@oldboy ~]# awk -F  '[ :]'  '/Xiaoyu/{print "$"$5"$"$6"$"$7}' reg.txt 
    $155$90$201
    
    [root@oldboy ~]# awk -F '[ :]' -vOFS='$'  '/Xiaoyu/{print "$"$5,$6,$7}' reg.txt
    $155$90$201
    
    显示所有人的全名,以姓,名的格式显示,如Meng,Feixue
    [root@oldboy ~]# awk   '{print $1","$2}'  reg.txt 
    Zhang,Dandan
    Zhang,Xiaoyu
    Meng,Feixue
    Wu,Waiwai
    Liu,Bingbing
    Wang,Xiaoai
    Zi,Gege
    Li,Youjiu
    Lao,Nanhai
    [root@oldboy ~]# awk -vOFS=','  '{print $1,$2}' reg.txt
    Zhang,Dandan
    Zhang,Xiaoyu
    Meng,Feixue
    Wu,Waiwai
    Liu,Bingbing
    Wang,Xiaoai
    Zi,Gege
    Li,Youjiu
    Lao,Nanhai
    
    [root@oldboy ~]# awk   '{print $1,$2}'  reg.txt  |tr ' ' ','
    Zhang,Dandan
    Zhang,Xiaoyu
    Meng,Feixue
    Wu,Waiwai
    Liu,Bingbing
    Wang,Xiaoai
    Zi,Gege
    Li,Youjiu
    Lao,Nanhai
    

    取ip地址

    [root@oldboy ~]# ifconfig eth0
    eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 10.0.0.150  netmask 255.255.255.0  broadcast 10.0.0.255
            inet6 fe80::80b2:dc89:7adf:c7b1  prefixlen 64  scopeid 0x20<link>
            ether 00:0c:29:1d:ca:3a  txqueuelen 1000  (Ethernet)
            RX packets 25397  bytes 2315305 (2.2 MiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 19057  bytes 2801352 (2.6 MiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    [root@oldboy ~]# ifconfig eth0 |awk 'NR==2{print $2}'
    10.0.0.150
    [root@oldboy ~]# ip a s eth0
    2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
        link/ether 00:0c:29:1d:ca:3a brd ff:ff:ff:ff:ff:ff
        inet 10.0.0.150/24 brd 10.0.0.255 scope global noprefixroute eth0
           valid_lft forever preferred_lft forever
        inet6 fe80::80b2:dc89:7adf:c7b1/64 scope link noprefixroute 
           valid_lft forever preferred_lft forever
    [root@oldboy ~]# ip a s eth0 |awk -F '[ /]*'  'NR==3{print $3}'
    10.0.0.150
    
    #排除空行 
    [root@oldboy ~]# awk  '!/^[ 	]*$/'  file1
    lhlgsd
    fdssgdgs
    gsdgd
    [root@oldboy ~]# awk  '!/^s*$/'  file1
    lhlgsd
    fdssgdgs
    gsdgd
    
    [root@oldboy ~]# awk '/^r/'  passwd 
    root:x:0:0:root:/root:/bin/bash
    redis:x:997:995:Redis Database Server:/var/lib/redis:/sbin/nologin
    rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
    rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
    roott
    [root@oldboy ~]# awk '/h$/'  passwd 
    root:x:0:0:root:/root:/bin/bash
    
    
    

    计算

    [root@oldboy ~]# cat test.log
    10 20
    [root@oldboy ~]# awk '{print $2/$1}' test.log
    2
    [root@oldboy ~]# awk '{print $2+$1}' test.log
    30
    [root@oldboy ~]# awk '{print $2-$1}' test.log
    10
    [root@oldboy ~]# awk '{print $2*$1}' test.log
    200
    [root@oldboy ~]# awk '{print $2^$1}' test.log
    10240000000000
    [root@oldboy ~]# awk  'BEGIN{print  10-10 }'
    0
    [root@oldboy ~]# awk  'BEGIN{print  10+10 }'
    20
    [root@oldboy ~]# awk  'BEGIN{print  10/10 }'
    1
    [root@oldboy ~]# awk  'BEGIN{print  10*10 }'
    100
    [root@oldboy ~]# awk  'BEGIN{print  10^10 }'
    10000000000
    
    #计算内存可用的百分比
    [root@oldboy ~]# free -m
                  total        used        free      shared  buff/cache   available
    Mem:           1980         148        1526           9         305        1646
    Swap:          2047           0        2047
    [root@oldboy ~]# free -m |awk '/^Mem/'
    Mem:           1980         148        1526           9         305        1646
    [root@oldboy ~]# free -m |awk '/^Mem/{print $4/$2}'
    0.770707
    [root@oldboy ~]# free -m |awk '/^Mem/{print $4/$2*100}'
    77.0707
    [root@oldboy ~]# free -m |awk '/^Mem/{print $4/$2*100"%"}'
    77.0707%
    
    
    

    Awk 数组

    #统计/etc/services 中空行的数量
    
    [root@oldboy ~]# awk  '/^$/'  /etc/services  |wc -l
    17
    [root@oldboy ~]# sed  -n  '/^$/p'  /etc/services  |wc -l
    17
    [root@oldboy ~]# grep -c '^$'  /etc/services 
    17
    [root@oldboy ~]# awk  '/^$/{i++}END{print i}'  /etc/services
    17
    
    #统计passwd文件中所有shell的数量
    [root@oldboy ~]# awk -F:  '{print $NF}' passwd |sort |uniq -c
          1 /bin/bash
          1 /bin/sync
          1 /sbin/halt
         32 /sbin/nologin
          1 /sbin/shutdown
    
    [root@oldboy ~]# awk -F:  '{shell[$7]++}END{for (i in shell)print i,shell[i]}' passwd  |sort -rnk2 |column  -t
    /sbin/nologin   32
    /sbin/shutdown  1
    /sbin/halt      1
    /bin/sync       1
    /bin/bash       1
    
    #计算
    [root@oldboy ~]# seq  200 |awk '{i=i+$1}END{print i}'
    20100
    [root@oldboy ~]# seq  100 |awk '{i=i+$1}END{print i}'
    5050
    [root@oldboy ~]# seq  101 |awk '{i=i+$1}END{print i}'
    5151
    
    
    
  • 相关阅读:
    邻接矩阵学习
    Spark on Yarn年度知识整理
    HBASE解析
    Storm的数据处理编程单元:Bolt 学习整理
    jsch ssh服务器调用Linux命令或脚本的小问题
    storm的数据源编程单元Spout学习整理
    Java 正则表达式详细使用
    【转】如何成为一个技术全面的架构师
    【转】Linux netstat命令详解,高级面试必备
    【转】Servlet 生命周期、工作原理
  • 原文地址:https://www.cnblogs.com/223zhp/p/11700373.html
Copyright © 2011-2022 走看看