awk之match函數


功能: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

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM