三剑客练习

root
233
文章
0
评论
2020年2月8日23:25:04 评论 3902字阅读13分0秒

三剑客练习

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
继续阅读
weinxin
我的微信
这是我的微信扫一扫
  • 文本由 发表于 2020年2月8日23:25:04
  • 除非特殊声明,本站文章均为原创,转载请务必保留本文链接
网站数据会滚脚本 shell

网站数据会滚脚本

网站数据会滚脚本 当代码经过测试环境后,正式进入生产环境,突然发现有问题,这时候需要立刻还原数据,写一个简单的网站代码回滚脚本 java项目的 #!/bin/bash Web_Dir=/server/...
awk练习 shell

awk练习

awk 1.列出UID=0的用户 # awk -F : '$3==0' /etc/passwd root:x:0:0:root:/root:/bin/bash 2.列出系统的用户 # awk -F :...
函数,数组 shell

函数,数组

shell中函数的作用 命令合集,完成特定功能的代码块,在shell中定义函数可以使用代码模块化,便于复用代码,加强可读性,函数和变量类似,先定义才可调用,如果定义不调用则不会被执行 传参 $1,$2...
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: