三剑客练习
grep
I am hewenfu teacher! I teach linux. test I like badminton ball ,billiard ball and chinese chinese chess! my blog is https:// www.hebbao.com our site is https:// www.hebbao.com my qq num is 30772818 not 2315242323131
过滤以my开头的行
[root@web01 ~]# grep ^my test.txt
my blog is https:// www.hebbao.com
my qq num is 30772818
过滤以m结尾的行
[root@web01 ~]# grep m$ test.txt
my blog is https:// www.hebbao.com
our site is https:// www.hebbao.com
排除/etc/ssh/sshd_config文件中的#号和空行,并打印行号
[root@web01 ~]# egrep -vn "^$|#" /etc/ssh/sshd_config
17:UseDNS no
18:GSSAPIAuthentication no
23:AddressFamily inet
27:HostKey /etc/ssh/ssh_host_rsa_key
29:HostKey /etc/ssh/ssh_host_ecdsa_key
30:HostKey /etc/ssh/ssh_host_ed25519_key
37:SyslogFacility AUTHPRIV
52:AuthorizedKeysFile .ssh/authorized_keys
70:PasswordAuthentication yes
74:ChallengeResponseAuthentication no
84:GSSAPIAuthentication yes
85:GSSAPICleanupCredentials no
101:UsePAM yes
106:X11Forwarding yes
120:UseDNS no
131:AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
132:AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
133:AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
134:AcceptEnv XMODIFIERS
137:Subsystem sftp /usr/libexec/openssh/sftp-server
排除空行
方法一: [root@web01 ~]# grep " " test.txt I am hewenfu teacher! I teach linux. I like badminton ball ,billiard ball and chinese chinese chess! my blog is https:// www.hebbao.com our site is https:// www.hebbao.com my qq num is 30772818 not 2315242323131 方法二: [root@web01 ~]# grep "." test.txt I am hewenfu teacher! I teach linux. test I like badminton ball ,billiard ball and chinese chinese chess! my blog is https:// www.hebbao.com our site is https:// www.hebbao.com my qq num is 30772818 not 2315242323131
排除数字
[root@web01 ~]# grep [^0-9] test.txt
I am hewenfu teacher!
I teach linux.
test
I like badminton ball ,billiard ball and chinese chinese chess!
my blog is https:// www.hebbao.com
our site is https:// www.hebbao.com
my qq num is 30772818
not 2315242323131
匹配数子
[root@web01 ~]# grep [0-9] test.txt my qq num is 30772818 not 2315242323131
sed
删除多行,指定行
[root@web01 ~]# sed -n '1,4p' test.txt I am hewenfu teacher! I teach linux. test [root@web01 ~]# sed '1,4d' test.txt I like badminton ball ,billiard ball and chinese chinese chess! my blog is https:// www.hebbao.com our site is https:// www.hebbao.com my qq num is 30772818 not 2315242323131 删除指定行 [root@web01 ~]# sed -n '1,3p' test.txt I am hewenfu teacher! I teach linux. test [root@web01 ~]# sed -n '1p;3p' test.txt I am hewenfu teacher! test [root@web01 ~]# sed '1d;3d' test.txt I teach linux. I like badminton ball ,billiard ball and chinese chinese chess! my blog is https:// www.hebbao.com our site is https:// www.hebbao.com my qq num is 30772818 not 2315242323131
打印有www.hebbao.com相关信息
[root@web01 ~]# sed -n /hebbao/p test.txt
my blog is https:// www.hebbao.com
our site is https:// www.hebbao.com
删除配置文件中#号开头的注释行,如果碰到tab或空格是无法删除
[root@web01 ~]# sed '/#/d' /etc/ssh/sshd_config
删除配置文件中tab行
[root@web01 ~]# sed '/^[\t]/'d nginx_up.sh
#!/bin/bash
Nginx_restart(){
}
Nginx_Stop(){
}
Nginx_Start(){
}
Nginx_reload(){
}
Nginx_Status(){
}
nginx(){
cat<<x
x
}
删除配置文件中tab行与空行
[root@web01 ~]# cat test.txt
I am hewenfu teacher!
I teach linux.
test
I like badminton ball ,billiard ball and chinese chinese chess!
my blog is https:// www.hebbao.com
our site is https:// www.hebbao.com
my qq num is 30772818
not 2315242323131
[root@web01 ~]# sed '/^$/d;/^[\t]/d' test.txt
I am hewenfu teacher!
I teach linux.
test
I like badminton ball ,billiard ball and chinese chinese chess!
my blog is https:// www.hebbao.com
our site is https:// www.hebbao.com
给文件内www.hebbao.com信息的行加#
[root@web01 ~]# sed '/hebbao/s#^#\##g' test.txt
I am hewenfu teacher!
I teach linux.
test
I like badminton ball ,billiard ball and chinese chinese chess!
#my blog is https:// www.hebbao.com
#our site is https:// www.hebbao.com
my qq num is 30772818
not 2315242323131
awk
处理前 BEGIN{ }
处理中 { }
处理后 END{ }
更多的请看awk练习
[root@web01 ~]# awk 'BEGIN{print 1+1}' 2 [root@web01 ~]# awk 'BEGIN{print 1/2}' 0.5
在处理前输出第一列信息
[root@web01 ~]# awk 'BEGIN{FS=":"}{print $1}' /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
。。。。
效果一样
[root@web01 ~]# awk -F: '{print $1}' /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
。。。
awk可以做sed的查看功能
[root@web01 ~]# awk '/hebbao/' test.txt my blog is https:// www.hebbao.com our site is https:// www.hebbao.com [root@web01 ~]# awk '/hebbao/{print $1}' test.txt my our
继续阅读

我的微信
这是我的微信扫一扫
评论