linux正则表达式BRE
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux正则表达式BRE相关的知识,希望对你有一定的参考价值。
是一套处理字符串的柜子和方法,以行为单位对字符串进行处理
通过特殊的符号的辅助,可以快速过滤 替换某些特定的字符串
awk sed grep
^word 已word开头
wrod$ 已word 结尾
# grep "^I" hequan # grep -i "^i" hequan 不区分大小写
# grep -i ".$" hequan
. 代表只能代表任意一个字符
# grep "li.e" hequan
\ 转移符号,取消原来字符的特殊意义、
# grep -n "\." hequan
*| 重复0个 或多个前面的一个字符。
# grep "490*448" hequan
49000000000000048,49448
# grep "49.*448" hequan # grep ".*" hequan 所有 # grep "^.*" hequan
[]. 字符集合的重复特殊字符, # grep "h[ex]quan" hequan 查找hequan 或者hxquan
[^word] 匹配不包括^后面的任意字符
# grep "[^not boog]" hequan --color=auto
I am hequan linux teacher.
I like chinese chess, table tennis.
111111111111111
49000000000000048,49448
# grep -E "[^0-9|,]" hequan --color=auto
a\{n,m\} 重复n到m次,前一个重复的的字符
a{n,} 重复至少n次
a{n} 重复n次
# grep "490\{2,3\}" hequan
# grep "490\{1,\}" hequan
# grep "490\{4\}" hequan --color=auto
扩展正则表达式:ERF
+ 重复一个或者一个以上前面的字符
# egrep "490+" hequan
? 重复0个或者一个前面的字符
| 用或的方式查找多个符号的字符串
() 找出“用户组”字符串
# grep -E "h(ex|equa)n" hequan
man grep
/REGULAR
# ifconfig eth0 | grep "inet addr" |cut -d ":" -f2 | cut -d " " -f1
192.168.10.10
# ifconfig eth0 | grep "inet addr" |awk -F ":" ‘{print $2}‘ | awk ‘{print $1}‘
# ifconfig eth0 | grep "inet addr" | awk -F "[: ]" ‘{print $13}‘
192.168.10.10
# ifconfig eth0 | grep "inet addr" | awk -F "[: ]+" ‘{print $4}‘
# echo ---1:----2 | awk -F ‘[-:]+‘ ‘{print $3}‘
2
# ifconfig eth0 | sed -n ‘2p‘
awk -F "[-:]+"
$1 $NF 最后一列 $0整行 NF NR
# ifconfig eth0 | sed -n ‘/inet addr/p‘
# ifconfig eth0 | sed -n ‘/inet addr/p‘ |sed ‘s#^.*addr:##g‘
# ifconfig eth0 | sed -n ‘/inet addr/p‘ |sed ‘s#^.*addr:##g‘ | sed ‘s# Bc.*$##g‘
# ifconfig eth0 | grep "HWaddr" |awk -F "[ ]+" ‘{print $5}‘
00:0C:29:E1:35:13
.*
# ifconfig eth0 | sed -n ‘s#^.*addr:\(.*\) Bcast.*$#\1#gp‘
192.168.10.10
# ifconfig eth0 | sed -n ‘s#^.*HWaddr \(.*\)#\1#gp‘
00:0C:29:E1:35:13
# sed ‘s#\(dd\) \(xx\)#\1#g‘ a.txt
dd
# ifconfig eth0 | sed -n ‘s#^.*dr:\(.*\) Bcast:\(.*\) Ma.*$#\1 \2#gp‘
192.168.10.10 192.168.10.255
# ifconfig eth0 | sed -n ‘s#^.*HWaddr \(.*\)#\1#gp‘
00:0C:29:E1:35:13
4 2 1 r w x
# stat /xx
# stat /xx |sed -n 4p| awk -F "[/(]" ‘{print $2}‘
0644
# stat /xx | head -4 | tail -1 | awk -F "[/(]" ‘{print $2}‘
0644
# stat /xx | sed -n ‘s#^.*ess: (0\(.*\)/-.*$#\1#gp‘
644
# stat /xx | sed -n ‘s#^.*(\(.*\)/-.*$#\1#gp‘
0644
# stat -c %a /xx
644
# stat -c %A /xx
-rw-r--r--
# stat -c %B /xx
512
# stat -c %b /xx
0
# stat /xx | sed -n ‘4p‘ | awk -F "[(/]" ‘{print $2}‘
0644
# ll /xx | cut -c 2-10 | tr rwx- 4210 | awk -F "" ‘{print $1+$2+$3 $4+$5+$6 $7+$8+$9}‘
644
# vi /etc/resolv.conf
vi /etc/sysconfig/network-scripts/ifcfg-eth0
chmod 644 hequan
# echo -n hequan;echo hequan 不换行
hequanhequan
# echo -e "hequan\thequan"
hequan hequan
# echo -e "hequan\nhequan"
hequan
hequan
# LANG=en
# date ‘+%y-%m-%d‘
16-03-30
# date ‘+%Y-%m-%d‘
2016-03-30
# date +%w
3
# date +%u
3
cut
# echo `date +%F\ %T`
2016-03-30 17:06:52
tar
# tar zcvf /tmp/qwe2.tar.gz ./etc/
# tar zcvf /tmp/qwe-$(date +%F-%T).tar.gz ./etc/
# tar zcvf /tmp/qwe-$(date +%w).tar.gz ./etc/
# find /log -type f -name "*.log" -mtime +7 |xargs rm -f
# /usr/sbin/ntpdate time.ntvs.gov
# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
# cal
# which ifconfig
/sbin/ifconfig
# export PATH=$PATH:$HOME/bin:/sbin
# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin:/sbin
# vi /etc/profile
# date +%F --date=‘3 day ago‘
2016-03-24
# grep -v "^$" xx
hequan
hequan
# sed ‘/^$/d‘ xx
# awk /^[^$]/ xx
which ifconfig
whereis
find
locate -n 1 cp
# find / -name "cp"
who
lastlog
shutdown -h now halt poweroff
init 6
logout
exit
以上是关于linux正则表达式BRE的主要内容,如果未能解决你的问题,请参考以下文章
linux sed命令中的正则表达式问号加号圆括号等需要转义