Wednesday, June 25, 2014

awk: string functions(1)

1. Substring
script_1:
 #! /bin/bash  
   
 awk 'BEGIN {  
   print substr("Hello", 1, 2);  
   #Output: He  
   #Meaning: start from first character, extract 2 chars  
   
   print substr("Hello", 1);  
   #Output: Hello  
   #If omitting the 3rd parameter, will just extract remaining  
   #characters  
   
   print substr("Hello", 0);  
   print substr("Hello", -1, -2);  
   #substr starts the index from 1 instead of 0, if index is  
   #out-of-bounds, the output is implementation defined  
 }'  

2. letter case conversion
script_1:
 #! /bin/bash  
   
 awk 'BEGIN {  
   print tolower("ABCdef(123)");  
   print toupper("ABCdef(123)");  
   
   #output:  
   #abcdef(123)  
   #ABCDEF(123)  
 }'  

3. string searching
script_1:
 #! /bin/bash  
   
 awk 'BEGIN {  
   print index("Hi world!", "world");  
   #output 4;  
   #This means, awk still start the index from 1  
   # instead of 0  
   
   print index("Hi world!", "WORLD");  
   #output 0;  
   #meaning not found the string  
   
   print index(tolower("Hi world!"), tolower("WORLD"));  
   #output 4;  
   #Ignore the letter case, by converting them to lower case  
   
   print rindex("Hi world Hi world!", "world");  
   #output 13  
   #Self-made the find the index from the end of string  
 }  
   
 function rindex(str, find, k, ls, lf)  
 {  
   ls=length(str);  
   lf=length(find);  
   
   if(lf>ls) return 0;  
   
   for(k=ls-lf+1;k>0;k--)  
   {  
     if(substr(str, k, lf)==find)  
       return k;  
   }  
   
   return 0;  
 }  
 '  

4. String matching
script_1:
 #! /bin/bash  
   
 awk 'BEGIN {  
   str="Hello world! Amazign world!";  
   print str ~ "w.*!";  
   #output 1  
   #meaning: str match the regular expression "w.*!"  
   
   print str ~ "k.*!";  
   #output 0  
   #meaning: str does not match the regular exprssion  
   
   print match(str, "world!");  
   #output 7  
   #meaning: str match the regular expression, and   
   #starting index is 7  
   
   print RSTART, RLENGTH;  
   #Output: 7 6  
   #The side effect of match function is updating the  
   #variable RSTART and RLENGTH, which means starting  
   #index and length of matched string  
   
   print substr(str, RSTART, RLENGTH);  
   #output: world!  
   #We can use the RSTART and RLENGTH to extract matched  
   #string  
 }'  

No comments:

Post a Comment