Content added Content deleted
imported>DrOwl No edit summary |
imported>DrOwl mNo edit summary |
||
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> |
Revision as of 23:59, 11 November 2003
AWK its big ... a full on ProgrammingLanguage really
a GooD place to get more info is in the standard *nix ManCommand
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...