正则表达

正则表达:

${a:- } 定义初值
基本正则
^
$
[]
[^]
.

  •                                     [不能单独使用]
    

{n,m}
{n} : 转义符
{n,}
() 保留 grep “(xjd:){2}” xx
扩展正则
+ 至少匹配一次
? 最多匹配一次
{n,m} n到m次
() 保留,组合为整体
| 或者
\b 单词边界
\B 非单词边界
\d 匹配一个数字字符
\D 匹配一个非数字字符
\n 换行符
\f 分页符
\s 任何一个非可见的字符
\S 任何一个可见的字符
\t z制表符
\w 任何一个单词字符
\W
< 开头
> 结尾

|wc -l 统计
egrep -v 取反 -i 忽略大小 -c 统计 -m2 只查前两行


sed 流式编辑器 stream editor
1.前置指令 | sed
2.sed 选项 (定址符)指令 文件
选项 -n 屏蔽默认输出 -r支持扩展正则 -i 写入文件
指令 p 输出 s 替换 d 删除


[root@localhost opt]# sed ‘p’ u
t❌0:0:root:/root:/bin/bash
t❌0:0:root:/root:/bin/bash

[root@localhost opt]# sed -n ‘p’ u
t❌0:0:root:/root:/bin/bash

[root@localhost opt]# sed -n ‘2p’ u # 2: 指定行号
t❌0:0:root:/root:/bin/bash

[root@localhost opt]# sed -n ‘2~2p’ u 步长为2
t❌0:0:root:/root:/bin/bash
daemon❌2:2:daemon:/sbin:/sbin/nologin
lp❌4:7:lp:/var/spool/lpd:/sbin/nologin

[root@localhost opt]# sed -n ‘/root/p’ u 搜索指定内容
t❌0:0:root:/root:/bin/bash

[root@localhost opt]# sed -n ‘$=’ u 查看行号
8

[root@localhost opt]# sed ‘1d’ u 不用屏蔽输出
bin❌1:1:bin:/bin:/sbin/nologin
daemon❌2:2:daemon:/sbin:/sbin/nologin

[root@localhost opt]# sed -i ‘2,7d’ u -i 执行真实操作 并且不会显示
[root@localhost opt]# cat u
t❌0:0:root:/root:/bin/bash


[root@localhost opt]# sed ‘s/2017/xxxx/’ 1.txt 默认只换第一个
xxxx 2011 2018
xxxx 2017 2024
xxxx 2017 2017

[root@localhost opt]# sed ‘s/2017/xxxx/2’ 1.txt 换第二列的
2017 2011 2018
2017 xxxx 2024
2017 xxxx 2017

[root@localhost opt]# sed ‘3s/2017/xxxx/2’ 1.txt
2017 2011 2018
2017 2017 2024
2017 xxxx 2017

[root@localhost opt]# sed ‘/2018/s/2017/xxxx/’ 1.txt 找到有2018的行
xxxx 2011 2018
文件系统
▪ cat ▪ cd ▪ chmod ▪ chown
▪ chgrp ▪ cksum ▪ cmp ▪ cp
▪ du ▪ df ▪ fsck ▪ fuser
▪ ln ▪ ls ▪ lsattr ▪ lsof
▪ mkdir ▪ mount ▪ mv ▪ pwd
▪ rm ▪ rmdir ▪ split ▪ touch
▪ umask

程序
▪ at ▪ bg ▪ chroot ▪ cron
▪ exit ▪ fg ▪ jobs ▪ kill
▪ killall ▪ nice ▪ pgrep ▪ pidof
▪ pkill ▪ ps ▪ pstree ▪ sleep
▪ time ▪ top ▪ wait
使用环境
▪ env ▪ finger ▪ id ▪ logname
▪ mesg ▪ passwd ▪ su ▪ sudo
▪ uptime ▪ w ▪ wall ▪ who
▪ whoami ▪ write
文字编辑
▪ awk ▪ comm ▪ cut ▪ ed
▪ ex ▪ fmt ▪ head ▪ iconv
▪ join ▪ less ▪ more ▪ paste
▪ sed ▪ sort ▪ strings ▪ talk
▪ tac ▪ tail ▪ tr ▪ uniq
▪ vi ▪ wc ▪ xargs
Shell 程序
▪ alias ▪ basename ▪ dirname ▪ echo
▪ expr ▪ false ▪ printf ▪ test
▪ true ▪ unset
网络
▪ inetd ▪ netstat ▪ ping ▪ rlogin
▪ netcat ▪ traceroute
搜索
▪ find ▪ grep ▪ locate ▪ whereis
▪ which
杂项
▪ apropos ▪ banner ▪ bc ▪ cal
▪ clear ▪ date ▪ dd ▪ file
▪ help ▪ info ▪ size ▪ lp
▪ man ▪ history ▪ tee ▪ tput
▪ type ▪ yes ▪ uname ▪ whatis
###############################################################
ftp file Transfer protocol
############################################################
高级应用
[root@localhost opt]# sed -r ‘s/(a)(b)©/\3\2\1/’ 1.txt 替换顺序
cba
cdb

[root@localhost opt]# sed -r ‘s/[0-9]//g’ 1.txt 删除所有数字
abc ewtawsagerha wsetsa
fdb sagewgsaga

[root@localhost opt]# sed -r ‘s/([0-9])/(\1)/g’ 1.txt 加括号
abc ewt(6)aw(5)sa(3)g(2)e(6)r(5)ha ws(4)et(5)sa(1)(2)
f(6)d(6)(4)b sag(5)ew(2)g(1)(5)sag(6)(3)a


i: 在指定行之前 添加
a: 在指定行之后 添加
c: 替换指定的行
[root@localhost opt]# sed ‘i 666’ 1.txt
666
abc ewt6aw5sa3g2e6r5ha ws4et5sa12
666
f6d64b sag5ew2g15sag63a

[root@localhost opt]# sed ‘2c 666’ 1.txt
abc ewt6aw5sa3g2e6r5ha ws4et5sa12
666


#!/bin/bash
#grep ‘bash ′ / e t c / p a s s w d ∣ s e d ′ s / : . ∗ / / ′ u = ‘ s e d − n ′ / b a s h ' /etc/passwd | sed 's/:.*//' u=`sed -n '/bash /etc/passwdseds/:.//u=sedn/bash/s/:.*//p’ /etc/passwdfor i in $u do s=grep " i " / e t c / s h a d o w ‘ p a s s = i" /etc/shadow` pass= i"/etc/shadowpass={s#root:} 掐头
pass1=KaTeX parse error: Expected '}', got 'EOF' at end of input: … 去尾 echo 'i’ --> $pass1
done
############################################################
awk 精确搜索
1.前置指令 | awk 选项 (条件)指令
2.awk 选项 被处理文档
-F 修改分隔符
print


[root@localhost opt]# awk ‘{print $1}’ test 第一列
hello
welcome
[root@localhost opt]# awk ‘{print NR,NF}’ test 行号,列号
1 3 (NR: number of record 当前正在处理的行)
2 3 (NF :number of fileds 当前列有多少列数据)

[root@localhost opt]# head -2 /etc/passwd > uih
[root@localhost opt]# cat uih
root❌0:0:root:/root:/bin/bash
bin❌1:1:bin:/bin:/sbin/nologin
[root@localhost opt]# awk -F: ‘{print $2}’ uih 改掉 : 分隔符
x
x

[root@localhost opt]# df -h | awk ‘//$/{print $4}’
29G


[root@localhost opt]# tail -3 /var/log/secure
Apr 10 15:34:34 localhost login: ROOT LOGIN ON ttyS0
####################################3
BEGIN{} 初始任务,只执行一次
END{} 等逐行任务之后执行,只执行一次

awk ‘BEGIN{ }{ }END{ }’


条件
1.使用正则
/ /{print }

[root@localhost opt]# awk ‘/root/’ k (如果只是print,可以省略)
root❌0:0:root:/root:/bin/bash

[root@localhost opt]# awk -F: ‘$1~/bin/’ k $1~ 在第一列中包含bin 的
bin❌1:1:bin:/bin:/sbin/nologin

[root@localhost opt]# awk -F: ‘$1!~/bin/’ k ! 取反 # 不包含bin的
root❌0:0:root:/root:/bin/bash
daemon❌2:2:daemon:/sbin:/sbin/nologin
adm❌3:4:adm:/var/adm:/sbin/nologin
lp❌4:7:lp:/var/spool/lpd:/sbin/nologin

  1. 数值/字符串
    == != > >= < <=
    [root@localhost opt]# awk -F: ‘NR5’ k 行号等于5的
    lp❌4:7:lp:/var/spool/lpd:/sbin/nologin
    [root@localhost opt]# awk -F: "$3
    ’4’’" k 第三列为4
    lp❌4:7:lp:/var/spool/lpd:/sbin/nologin (注意和sed比较)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值