Thursday, November 27, 2008

Awk pattern matching over a particular field


1 #!/bin/bash
2
3 echo babbbbil 12223 | awk '{for(i=1;i<=NF;i++){if($i ~ "122"){print $i;i=NF+1}}}'
4

This piece of awk code will go through all the fields and look for pattern "122" and print them. The "i=NF+1" will work like "break;" in C which will make awk to stop printing after the first hit. Happy Awk'ing ;)


0 comments:

Post a Comment