正则表达式的贪婪匹配

str="uid=100(guest) gid=100(others) groups=10(users),11(floppy)"
guest  others  users  floppy
正则表达式是有贪婪性的,它总是与最长的可能长度匹配,而且越是排在前面的通配符优先级越高。
 
#!/bin/bash
stra="uid=100(guest) gid=100(others) groups=10(users),11(floppy)"
strb="uid=100(guest)gid=100(others)groups=10(users),11(floppy)"
 
#floppy
echo $stra |sed 's/.*(\(.*\)).*/\1/'
echo $stra |awk '{a=substr($3,length($3)-6,6); b=substr($3,1,6); c=length($3);print a,b,c}'
#guest
echo $stra |sed 's/[^(]*(\([^)]*\)).*/\1/'
echo $stra |awk '{a=gensub(/.*\((.*)\)/,"\\1","g",$1);print a}'
#others
echo $stra |sed 's/users\|others/***&***/g'
echo $stra |sed 's/[^(]*([^)]*)[^(]*(\([^)]*\)).*/\1/'
echo $stra |sed 's/[^ ]*[ ][^(]*(\([^)]*\)).*/\1/'
#users
echo $stra |sed 's/.*[ ][^(]*(\([^)]*\)).*/\1/'
echo $stra |awk '{a=gensub(/[^(]*\(([^)]*).*/,"\\1","g",$3);print a}'
 
时间一久自己都看的晕忽忽的。。。记下来用的时候翻翻

你可能感兴趣的:(shell,awk,正则,sed,贪婪)