Create a new article
Write your page title here:
We currently have 3185 articles on s23. Type your article name above or click on one of the titles below and start writing!


Awk pattern matching

(Redirected from AwkPatternMatching)

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

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


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 }'
-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...

awk -F, ' {
  if ( $2 ~ /^StartString/ && $2 ~ /EndString$/ ) {
    print $2
} '  InPut.txt > OutPut.txt


 -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