-
Notifications
You must be signed in to change notification settings - Fork 0
awk
Kyle Coberly edited this page Aug 13, 2017
·
1 revision
pattern { action }
BEGIN pattern { action } { action } END pattern { action }
-
print- The action to print -
/t- Tab - Invoke
awk -F,to comma-separate
-
word ~ /(first_word|second_word)/- Matches -
word !~ /WORD/- Doesn't match
-
'nests -
exit- Early-exit the script
#!/bin/awk -f
BEGIN { print "File\tOwner" }
{ print $8, "\t", $3}
END { print " - DONE -" } #!/bin/sh
awk '{print $'$column'}'-
name="Kyle"- Assignment -
$0is the whole line -
$1,$4, etc. - Gives you each column -
$name- Named variable - Globals
- You can use more than one character
-
NF- Number of fields -
NR- Number of records, or the current record -
ORSRecord separator -
FS- Column separator -
FILENAME- Current filename -
ARGV[1]- An array of arguments -
ENVIRON["name"]- Array of variables
#!/bin/sh
column=${1:-1} # 1 is first variable passed in, -1 is default value
awk '{print $'$column'}'#!/bin/awk
if (conditional){
} else if {
} else {
}while ( conditional ) {
next
}
for ( expression ; conditional ; expression ) {
continue
}
for ( variable in array ) statement {
break
}substr(string,position,length)split(string,array,separator)tolower(string)toupper(string)sub(/pattern/, replacement, string)-
match(string, pattern)-
RSTART- Index of start -
RLENGTH- Length of match
-
-
system("unix_command")- Returns exit code function(parameter){}
#!/bin/awk -f
# Doesn't process any text
BEGIN {
print "File\tOwner"
exit;
}- You can't interpolate variables in actions