简单的linux系统学习笔记——11

一、1.特殊符号(续)

重定向

标准输出重定向【stdout:代码是1】
>   //标准输出【覆盖】重定向
>>  //标准输出【追加】重定向

标准输入重定向【stdin:代码是0】
<   //标准输入重定向
<<  

[root@c7-100 ~]# cat ceshi.txt
1111

[root@c7-100 ~]# cat < ceshi.txt
1111

----------------------------------------------
【tr】替换命令

【【【 tr “要替换的(原本)” “想要替换的内容” 】】】

[root@c7-100 ~]# echo "111 222 333" > 1.txt
[root@c7-100 ~]# cat 1.txt
111 222 333
[root@c7-100 ~]# tr " " "\n" 1.txt
tr: 额外的操作数 "1.txt"
Try 'tr --help' for more information.
[root@c7-100 ~]# tr " " "\n" < 1.txt
111
222
333

[root@c7-100 ~]# echo "oldboy12345" | tr "boy12" "yob21"
oldyob21345
------------------------------------------------
xargs 案例
[root@c7-100 ~]# xargs -n 1 < 1.txt   //-n换行
111
222
333

重要的使用方式

正确的输出,写入文件
[root@c7-100 ~]# echo 111 > 1.txt
[root@c7-100 ~]# ech00 111 >> 1.txt
-bash: ech00: 未找到命令
[root@c7-100 ~]# cat 1.txt
111

[root@c7-100 ~]# ech00 111 1>> 1.txt   //正确输出尖角号前默认有个1代表正确输入

错误的输出,写入文件
[root@c7-100 ~]# echoo 111 2> 1.txt   //错误输出尖角号前加个2代表错误输入
[root@c7-100 ~]# echo 111 2>> 1.txt
111
[root@c7-100 ~]# cat 1.txt
-bash: echoo: 未找到命令

错误的与正确的都写入文件
[root@c7-100 ~]# echoo 111 &>> 1.txt  //&>  &>> 都可以将错误信息或者普通信息都重定向输出
[root@c7-100 ~]# echo 111 &>> 1.txt
[root@c7-100 ~]# cat 1.txt 
-bash: echoo: 未找到命令
111

拓展:正确的错误的都写入文件
2>&1>

二、系统正则符号

通配符:查文件名(find)

正则:查文件内容(三剑客)grep、sed、awk

正则分为{

1.基础正则

2.拓展正则

}        

准备环境

[root@c7-100 ~]# cat 1.txt
I am wa
i love my job.

my id 100869874561
My name is oldwa

my tel id is 10086

my ipaddress 192.168.17.197

1.【^】以什么开头

1.筛选出以大写M开头的行
[root@c7-100 ~]# grep "^M" 1.txt 
My name is oldwa

2.筛选出以小写m开头的行
[root@c7-100 ~]# grep "^m" 1.txt
my id 100869874561
my tel id is 10086
my ipaddress 192.168.17.197

2.【$】以什么结尾

筛选出以小写b结尾的行
[root@c7-100 ~]# grep "b$" 1.txt
i love my job.

筛选出空行(除了空行)

grep -v 取反

[root@c7-100 ~]# grep "^$" 1.txt



[root@c7-100 ~]# grep -v "^$" 1.txt
I am wa
i love my job.
my id 100869874561
My name is oldwa
my tel id is 10086
my ipaddress 192.168.17.197
-----------------------------------------------
[root@c7-100 ~]# grep -v "^#" /etc/selinux/config 
SELINUX=disabled
SELINUXTYPE=targeted 

3.【.】匹配任意一个字符,有且只有一个

筛选,以任意一个字符开头的行
[root@c7-100 ~]# grep "^." 1.txt
I am wa
i love my job.
my id 100869874561
My name is oldwa
my tel id is 10086
my ipaddress 192.168.17.197

筛选,以任意一个字符结尾的行
[root@c7-100 ~]# grep ".$" 1.txt
I am wa
i love my job.
my id 100869874561
My name is oldwa
my tel id is 10086
my ipaddress 192.168.17.197

筛选文件中任意一个字符行
[root@c7-100 ~]# grep "." 1.txt
I am wa
i love my job
my id 100869874561
My name is oldwa
my tel id is 10086
my ipaddress 192.168.17.197

筛选以【.】结尾的行
[root@c7-100 ~]# grep "\.$" 1.txt
i love my job.


[root@c7-100 ~]# cat 3.txt
as adab
wada泰拉
adadsfgds
[root@c7-100 ~]# egrep "[a-z]{1}d[a-z]{5}.s"  3.txt   //以任意字母开头连续1次,中间是d,以任意字母开头连续5次结尾时.s的行
adadsfgds

[root@c7-100 ~]# egrep "^(.d).*(.s)$"  3.txt  //以.d开头的以.s结尾的中间任意字符 .*代表任意数量的任意字符 
adadsfgds

4.【*】出现0次或0次以上

[root@c7-100 ~]# vim 1.txt     
[root@c7-100 ~]# cat 1.txt
I am wa
i love my job.

my id 100869874561
My name is oldwa

my tel id is 10086

my ipaddress 192.168.17.197
ahhhhhhhhhhhhhhhha
bbbbbbbbbbbbbba
筛选连续出现0次或0次以上的1的行
[root@c7-100 ~]# grep "1*" 1.txt
I am wa
i love my job.

my id 100869874561
My name is oldwa

my tel id is 10086

my ipaddress 192.168.17.197
ahhhhhhhhhhhhhhhha
bbbbbbbbbbbbbba

【.*】筛选连续出现0次或0次以上的任意的行
[root@c7-100 ~]# grep ".*" 1.txt
I am wa
i love my job.

my id 100869874561
My name is oldwa

my tel id is 10086

my ipaddress 192.168.17.197
ahhhhhhhhhhhhhhhha
bbbbbbbbbbbbbba

----------------------------------------------
[root@c7-100 ~]# cat 3.txt
bbbbbbbbbbbbbbbba
awdadbsadasfsdfs
as adab
[root@c7-100 ~]# egrep -o "ab*" 3.txt  
a       //先匹配到了a接着又开始找b但是找不到
a		//找到了a但找不到b
a		//找到了a但找不到b
a		//找到了a但找不到b
a		//找到了a但找不到b
a		//找到了a但找不到b
a		//找到了a但找不到b
ab		//找到了ab
[root@c7-100 ~]# egrep  "ab*" 3.txt
bbbbbbbbbbbbbbbba
awdadbsadasfsdfs
as adab

5.【[]】匹配中括号中任意一个字符

[root@c7-100 ~]# grep "[abc]" 1.txt
I am wa
i love my job.
My name is oldwa
my ipaddress 192.168.17.197
ahhhhhhhhhhhhhhhha
bbbbbbbbbbbbbba

[root@c7-100 ~]# grep  "[!abc]" 1.txt  等同于  grep  "[^abc]" 1.txt
I am wa
i love my job.
My name is oldwa
my ipaddress 192.168.17.197
ahhhhhhhhhhhhhhhha
bbbbbbbbbbbbbba


grep -o 查看匹配过程
[root@c7-100 ~]# grep -o "[abc]" 1.txt
a
a
b
a
a
a
a
a
b
b
b
b
b
b
b
b
b
b
b
b
b
b
a

三、系统扩展正则

注意:
在使用扩展正则的时候,grep是不好使的,需要使用grep -E 或者 egrep

1.【+】匹配前一个字符,出先一次或一次以上

筛选出连续出现1次或1次以上的h
[root@c7-100 ~]# grep -E  "h+" 1.txt   
ahhhhhhhhhhhhhhhha

筛选出连续出现0次或0次以上的h
[root@c7-100 ~]# egrep  "h*" 1.txt
I am wa
i love my job.

my id 100869874561
My name is oldwa

my tel id is 10086

my ipaddress 192.168.17.197
ahhhhhhhhhhhhhhhha
bbbbbbbbbbbbbba


------------------------------------------------------------

[root@c7-100 ~]# egrep  "ab*" 3.txt
bbbbbbbbbbbbbbbba
awdadbsadasfsdfs
as adab
[root@c7-100 ~]# egrep  "ab+" 3.txt   //+前面自带一个()小括号视为一个整体
as adab

2.【?】匹配前一个字符出现0次或1次的情况

[root@c7-100 ~]# egrep -o "h?" 1.txt
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
[root@c7-100 ~]# egrep -o "h*" 1.txt
hhhhhhhhhhhhhhhh

3.【|】或者

查看以大写小写i开头的行
[root@c7-100 ~]# egrep  "^I|^i" 1.txt
I am wa
i love my job.
查看以大写小写i开头的行【小括号,表示一个整体】
[root@c7-100 ~]# egrep  "^(I|i)" 1.txt
I am wa
i love my job.

------------------------------------------
匹配
[root@c7-100 ~]# grep  "^[Ii]" 1.txt
I am wa
i love my job.

4.【()】小括号

查看以大写小写i开头的行【小括号,表示一个整体】
[root@c7-100 ~]# egrep  "^(I|i)" 1.txt
I am wa
i love my job.

5.【a{n,m}】大括号

a:要查询的字符

n:连续出现n次最小

m:连续出现n次最大

查看连续出现两次0的行
[root@c7-100 ~]# egrep "0{2}" 1.txt
my id 100869874561
my tel id is 10086

筛选连续出现最少3次最大5次的行
[root@c7-100 ~]# egrep "z{3,5}" 1.txt
zzzd
zzzzd
zzzzzd
zzzzzzd
[root@c7-100 ~]# egrep -o "z{3,5}" 1.txt
zzz
zzzz
zzzzz
zzzzz


--------------------------------------------------
筛选连续出现最大4次的行
[root@c7-100 ~]# egrep -o "z{,4}" 1.txt
zz
zzz
zzzz
zzzz
z
zzzz
zz
[root@c7-100 ~]# egrep "z" 1.txt
zzd
zzzd
zzzzd
zzzzzd
zzzzzzd

6.【\b\b】边界符

[root@c7-100 ~]# cat 2.txt
111wahh111
wahh
2wahh2
[root@c7-100 ~]# egrep  "wahh" 2.txt
111wahh111
wahh
2wahh2
[root@c7-100 ~]# egrep  "\bwahh\b" 2.txt
wahh

四、总结grep命令

grep  -i  //不区分大小写
	  -o  //显示匹配过程
	  -v  //取反
	  -n  //显示行号
	  -E  //支持拓展正则
	  -w  //精确匹配,搜索啥,显示啥
[root@c7-100 ~]# grep "am" 1.txt
I am wa
My name is oldwa
[root@c7-100 ~]# grep -w "am" 1.txt
I am wa

五、sed替换命令(取行)

sed -n	//取消默认输出
	-r	//支持扩展正则
	-i	//修改文件内容
	-i.bak	//修改文件之前,先做个备份
	-d	//以行为单位删除

1.sed过滤文件

以行号方式筛选

查找文件的第n行
-n 取消默认输出;sed默认输出将文中所有内容都显示出来
[root@c7-100 ~]# sed -n "3p" passwd			//3p 打印第三行的内容
daemon:x:2:2:daemon:/sbin:/sbin/nologin

[root@c7-100 ~]# sed  "3p" passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
....

过滤出文件中第3行到第10行
[root@c7-100 ~]# sed -n "3,10p" passwd
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
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

以字符串方式筛选

【【【 sed + -n + "/字符串/p" + 文件路径 】】】
1.筛选带有sshd字符串的行
[root@c7-100 ~]# sed -n "/sshd/p" passwd
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

2.筛选带有sshd到wawa两个字符串中间的内容
[root@c7-100 ~]# sed -n "/sshd/,/wawa/p" passwd
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
wahh:x:1000:1000::/home/wahh:/bin/bash
ntp:x:38:38::/etc/ntp:/sbin/nologin
wa:x:1001:1001::/home/wa:/bin/bash
wawa:x:1111:0::/home/wawa:/bin/bash

2.sed替换,备份文件内容

s###g s///g s@@@g
s:sub替换
g:global全局替换

sed -i "s#SELINUX=enforcing#SELINUX=disabled#g" /etc/selinux/config

修改文件之前,做个备份【-i.bak】
[root@c7-100 ~]# sed -i.bak "s#SELINUX=enforcing#SELINUX=disabled#g" /etc/selinux/config
[root@c7-100 ~]# ll /etc/selinux/config*
-rw-r--r-- 1 root root 542 8月   1 15:35 /etc/selinux/config
-rw-r--r-- 1 root root 542 7月  25 16:31 /etc/selinux/config.bak


只替换某一行内容
[root@c7-100 ~]# cat 3.txt
as adab
wadasd
asd
adadsfgds
[root@c7-100 ~]# sed -i "2s#sd#泰拉#g" 3.txt   //在s###g前面加上行号
[root@c7-100 ~]# cat 3.txt
as adab
wada泰拉
asd
adadsfgds
[root@c7-100 ~]# cat 4.txt

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
#     泰拉 - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=泰拉 


[root@c7-100 ~]# sed -i "9,12s#泰拉#targeted#g" 4.txt    //9到12行
[root@c7-100 ~]# cat 4.txt

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 

3.删除文件内容

1.删除第一行
[root@c7-100 ~]# cat 3.txt
bbbbbbbbbbbbbbbba
awdadbsadasfsdfs
as adab
[root@c7-100 ~]# sed -i "1d" 3.txt    //【d】删除第几行就是几d
[root@c7-100 ~]# cat 3.txt
awdadbsadasfsdfs
as adab

删除空行和注释
[root@c7-100 ~]# cat 3.txt
#awdadbsadasfsdfs
as adab

wadasd

#3
asd
adadsfgds
#sdwff
[root@c7-100 ~]# sed -ri "/^$|^#/d" 3.txt  //正则扩展必须放在最前面,要不然不生效
[root@c7-100 ~]# cat 3.txt
as adab
wadasd
asd
adadsfgds

4.sed增加功能

a	//在某一行下追加
i	//在某一行上插入
c 	//替换某一行
【【【 sed + -i + "行号+参数+内容" + 文件路径 】】】
[root@c7-100 ~]# cat 4.txt 
111
222
333
444
[root@c7-100 ~]# sed -i "1a1.5" 4.txt 
[root@c7-100 ~]# cat 4.txt 
111
1.5
222
333
444
[root@c7-100 ~]# sed -i "1i0.5" 4.txt 
[root@c7-100 ~]# cat 4.txt 
0.5
111
1.5
222
333
444
[root@c7-100 ~]# sed -i "4c1.8" 4.txt   //没有的行无法替换
[root@c7-100 ~]# cat 4.txt 
0.5
111
1.5
1.8
333
444

5.反向引用

反向引用,分为两个步骤

1.分组

2.排序

[root@c7-100 ~]# cat 5.txt
123456
abcdef
[root@c7-100 ~]# sed -r "s#([0-9]+)#<\1>#g" 5.txt   //一个()分为一个组 \获取组  \1获取组1
<123456>
abcdef
[root@c7-100 ~]# sed -r "s#([0-3]+)([4-6]+)#<\1>#g" 5.txt
<123>
abcdef
[root@c7-100 ~]# sed -r "s#([0-3]+)([4-6]+)#<\1>\2#g" 5.txt
<123>456
abcdef
[root@c7-100 ~]# sed -r "s#([0-3]+)([4-6]+)#<\1>~\2~#g" 5.txt
<123>~456~
abcdef
-----------------------------------------------------------------

[root@c7-100 ~]# sed -r "s#([1-2]+)([3-4]+)([5-6]+)#\1-\2-\3#g" 5.txt
12-34-56
abcdef

[root@c7-100 ~]# sed -r "s#([1-2]+)([3-4]+)([5-6]+)#\2-\3-\1#g" 5.txt  //组替换内容的顺序可以换
34-56-12
abcdef

六、awk取行取列计算

取列

$NF	//列的总数(最后一列)
-F	//指定分隔符
NR==3  //取第三行

取第一列,第三列,最后一列

[root@c7-100 ~]# ll /etc/ | awk '{print $1,$3,$NF}'
总用量  1136
drwxr-xr-x. root 1
drwxr-xr-x. root 2
drwxr-xr-x. root abrt
-rw-r--r--. root adjtime
....

指定分隔符取列

[root@c7-100 ~]# awk -F ':' '{print $1,$6}' passwd
root /root
bin /bin
daemon /sbin
adm /var/adm
lp /var/spool/lpd
sync /sbin

练习:ip a 取出eth0网卡的ip地址,不要网段
[root@c7-100 ~]# ip a s eth0 | sed -n "3p" | awk '{print $2}' | awk -F '/' '{print $1}'
10.0.0.100

第二种方式
[root@c7-100 ~]# ip a s eth0 | awk NR==3 | awk '{print $2}' | awk -F '/' '{print $1}'
10.0.0.100

awk计算,内存空闲率

[root@c7-100 ~]# free | awk 'NR==2' | awk '{print $NF/$2*100"%"}'
72.6492%

awk过滤

[root@c7-100 ~]# awk "/sshd/,/wawa/" passwd
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
wahh:x:1000:1000::/home/wahh:/bin/bash
ntp:x:38:38::/etc/ntp:/sbin/nologin
wa:x:1001:1001::/home/wa:/bin/bash
wawa:x:1111:0::/home/wawa:/bin/bash

11-完

  • 8
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值