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(/.*\((.*)\)/," 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(/[^(]*\(([^)]*).*/," a}'
阅读(1474) | 评论(0) | 转发(0) |