有个文件,只有一行,内容如下:
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
请问怎么用awk显示出来这个呢?
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的服务器上竟然不能执行:
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 ,
最后发现其使用的是mawk:
2020 $ ll /etc/alternatives/awk
lrwxrwxrwx 1 root root 13 Aug 11 2014 /etc/alternatives/awk -> /usr/bin/mawk*
而ssp服务器上可以运行,使用的则是 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};}'
它比上面的方案更灵活在于他不需要指定域的具体位置,达到了match函数的匹配任意位置的功能。