×
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

    Difference between revisions of "Awk pattern matching"

    imported>DrOwl
     
    imported>mutante
    m (AwkPatternMatching moved to Awk pattern matching)
     
    (6 intermediate revisions by 2 users not shown)
    Line 1: Line 1:
    AWK its big ... a full on ProgrammingLanguage really
    +
    [[Awk|AWK]] its big ... a full on [[:Category:Programming Languages|programming language]] really
       
    a GooD place to get more info is in the standard *nix ManCommand
    +
    a GooD place to get more info is in the standard *nix 'man' command
     
    ----
     
    ----
     
    Examples
     
    Examples
    Line 17: Line 17:
     
    " " = a space... well any string
     
    " " = a space... well any string
     
    $7 = the 7th varible found
     
    $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...
      +
    <pre>
      +
    #!/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...
      +
      +
    </pre>
      +
      +
    also see: [[Awk]]
      +
      +
    [[Category:Linux]]
      +
    [[Category:Computer]]
      +
    [[Category:Unix]]
      +
    [[Category:Programming Languages]]

    Latest revision as of 21:28, 29 March 2006

    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.
    Cookies help us deliver our services. By using our services, you agree to our use of cookies.