有個(gè)文件依疼,只有一行,內(nèi)容如下:
AAA_FAH_Flashbackup.tl--------------------------------------xxxxxxxxxxxx--------NA,AAA_AIR_Classic_One_To_One.tl--------------------------------7.8_20150730--------this is some comments.,AAA_MP_MOptdup_.tl----------------------------------------7.8_20150716--------this is some comments,AAA_DP_AABAC.tl---------------------------------------------7.8_20150804--------This is some comments may, have
請(qǐng)問怎么用awk顯示出來這個(gè)呢而芥?
AAA_FAH_Flashbackup.tl律罢,AAA_AIR_Classic_One_To_One.tl,AAA_MP_MOptdup_.tl,AAA_DP_AABAC.tl
解決方案之一:
str='AAA_FAH_Flashbackup.tl--------------------------------------xxxxxxxxxxxx--------NA,AAA_AIR_Classic_One_To_One.tl--------------------------------7.8_20150730--------this is some comments.,AAA_MP_MOptdup_.tl----------------------------------------7.8_20150716--------this is some comments,AAA_DP_AABAC.tl---------------------------------------------7.8_20150804--------This is some comments may, have'
2695 $ echo $str|tr -s ',' "\n"|awk '{match($0,/([a-zA-Z_]*\.tl)/,a);print a[1]}'
AAA_FAH_Flashbackup.tl
AAA_AIR_Classic_One_To_One.tl
AAA_MP_MOptdup_.tl
AAA_DP_AABAC.tl
上面的方案在yunip的服務(wù)器上竟然不能執(zhí)行:
2013 $ echo $str|tr -s ',' "\n"|awk '{match($0,/([a-zA-Z_]*\.tl)/,a);print a[1]}'
awk: line 1: syntax error at or near ,
最后發(fā)現(xiàn)其使用的是mawk:
2020 $ ll /etc/alternatives/awk
lrwxrwxrwx 1 root root 13 Aug 11? 2014 /etc/alternatives/awk -> /usr/bin/mawk*
而ssp服務(wù)器上可以運(yùn)行,使用的則是 gawk :
2023 $ ll /etc/alternatives/awk
lrwxrwxrwx 1 root root 13 Jun 20 11:00 /etc/alternatives/awk -> /usr/bin/gawk*
解決方案之二:
grep -o 即可棍丐。
2028 $ echo $str|tr -s ',' "\n"|grep -E -o ".*\.tl"
AAA_FAH_Flashbackup.tl
AAA_AIR_Classic_One_To_One.tl
AAA_MP_MOptdup_.tl
AAA_DP_AABAC.tl
解決方案三:
echo $str|tr -s ',' '\n'|tr -s '-' ' '|awk '/\.tl/{print $1}'
解決方案之四:
echo $str|tr -s ',' '\n'|tr -s '-' ' '|awk '{for(i=1;i<NF;i++){if($i ~ /[a-zA-Z_]*\.tl/)print $i};}'
它比上面的方案更靈活在于他不需要指定域的具體位置误辑,達(dá)到了match函數(shù)的匹配任意位置的功能。