功能:match函數是用於個性化定制搜索模式。
例子:
文件內容:
this is wang ,not wan
that is chen, not che
this is chen ,and wang ,not wan che
思路:
比如你想提取is后面的第一個單詞,和not 后面的第一個單詞,
這時候利用位置來提取是不可行的,因為第三行的模式和前兩行不一致,這種情況在基因注解里經常會碰到。
這是就可以用awk的match函數啦!!
[wangjq@mgmt humandb]$ cat test this is wang,not wan that is chen,not che this is chen,and wang,not wan che [wangjq@mgmt humandb]$ awk '{match($0,/.+is([^,]+).+not(.+)/,a);print a[1],a[2]}' test wang wan chen che chen wan che
格式:match(string,regexp,array) 和string~regexp的作用類似
沒有array的情況下:通過regexp,在string中尋找最左邊,最長的substring,返回substring的index位置。
有array的情況下:在regexp中用()將要組成的array的內容按順序弄好,a[1]代表第一個()的內容,a[2]代表第二個()的內容,以此類推。
echo "gene_type "mrna";gene_name "typ""|awk 'match($0,/(gene_type).+(".+?");gene_name/,a){print a[1]}' gene_type echo "gene_type "mrna";gene_name "typ""|awk 'match($0,/(gene_type).+("+?");gene_nae/,a){print a[2]}' mrna