×
Create a new article
Write your page title here:
We currently have 3,185 articles on s23. Type your article name above or create one of the articles listed here!



    s23
    3,185Articles

    Awk pattern matching

    AWK its big ... a full on programming language really

    a GooD place to get more info is in the standard *nix 'man' command
    

    Examples

    to make a list of all the users and shell's that are in use on your box
     (not a verry practical example as there are non user enrtys in your passwd file but...)
    
    cat /etc/passwd | awk -F: ' {print $1 " " $7 }'
    
    Explanation
    
    -F: = use : as the separator ie use -F, for a csv file awk uses a space as standard
    print = print to StdOut
       $1 = the first varible found...
       " " = a space... well any string
       $7 = the 7th varible found
    


    This example is nice in a file ... it will serch through InPut.txt and put all lines that match into  OutPut.txt...
    
    #!/bin/ksh
    
    awk -F, ' {
      if ( $2 ~ /^StartString/ && $2 ~ /EndString$/ ) {
        print $2
      }
    } '  InPut.txt > OutPut.txt
    
    
    
      Explanation
    
     -F = as above use , as separator 
     if = well standard programing stuff really, if (this is true) { then do this}
      $2 = 2nd varible
      ~ = regexe ie patern matching (
         ^ = start of varible
         $ = end of string )
     && = logical and (for strings)
    
     basicly, if $2 starts with StartString and ends with EndString then print it out...
     
     

    also see: Awk

    Cookies help us deliver our services. By using our services, you agree to our use of cookies.