#From E. Quigley book
#To print all lines containing the pattern
awk '/pattern/' file
#To print the lines starting with the regex pattern1 and pattern2
awk '/^ (pattern1|pattern2)/' file
#To print the field 1 of lines starting with the regex pattern1
awk '/^ [pattern1]/{print $1}' file
#To print field 1 (or multiple fields, in particular orders)
awk '{ print $1}' file
awk '{ print $1, $2, $3}' file
awk '{ print $3, $2, $1}' file
#To print field 1 and 2 if the pattern is present
awk '/pattern/{ print $1, $2}' file
#To print the lines starting with the regex
awk '/^ pattern/{ print "Hello!"}' file
#Print entire file (with row number, with field number)
awk '{ print }' file
awk '{ print $0}' file
awk '{ print NR, $0}' file
awk '{ print NF, $0}' file
#Piping a command to awk
df | awk '$1 > 50000'
date | awk '{ print "Month: $2 "\nYear: " , $6}'
#Using escape sequence
awk '/pattern/ { print "\t\tHave a good day, "$1, $2 "\!"}' file
awk '/pattern/ { print "Hello there, "$1}' file
#Using OFMT variable to format decimal values
awk 'BEGIN{ OFMT ="%.2f" ; print 1.348973, 12E-2}' file
#Using prinf for fancy output (here spacing)
awk {printf "The name is: %10s ID is %8d\n", $1, $3}' file
#Changing field separator (-F is used to reassign the value of the separator) (here colon is separator)
awk -F: '{ print $1}' file
awk -F"[ :]" '{ print $1, $2}' file
awk -F: '/pattern/'{ print $1, $2}' file
#Print if $3 is less than 5000
awk '$3 < 5000' file
#Print lines starting with an uppercase letter, followed by smaller case letters, followed by space
awk '/^[A-Z][a-z]+ /' file
#Print lines with the patterns
awk '$1 ~ /[Dd]oll/' file
#Print lines without the pattern at the end
awk '$1 !~ /pattern$/' file
POSIX
#Print lines with bracketed characters added by POSIX
awk '$1 ~ /[[:lower:]]+g[[:space:]]+g[[:digit:]]/' file
#Print lines if column $3 has a literal period followed by number between the given range
#To print all lines containing the pattern
awk '/pattern/' file
#To print the lines starting with the regex pattern1 and pattern2
awk '/^ (pattern1|pattern2)/' file
#To print the field 1 of lines starting with the regex pattern1
awk '/^ [pattern1]/{print $1}' file
#To print field 1 (or multiple fields, in particular orders)
awk '{ print $1}' file
awk '{ print $1, $2, $3}' file
awk '{ print $3, $2, $1}' file
#To print field 1 and 2 if the pattern is present
awk '/pattern/{ print $1, $2}' file
#To print the lines starting with the regex
awk '/^ pattern/{ print "Hello!"}' file
#Print entire file (with row number, with field number)
awk '{ print }' file
awk '{ print $0}' file
awk '{ print NR, $0}' file
awk '{ print NF, $0}' file
#Piping a command to awk
df | awk '$1 > 50000'
date | awk '{ print "Month: $2 "\nYear: " , $6}'
#Using escape sequence
awk '/pattern/ { print "\t\tHave a good day, "$1, $2 "\!"}' file
awk '/pattern/ { print "Hello there, "$1}' file
#Using OFMT variable to format decimal values
awk 'BEGIN{ OFMT ="%.2f" ; print 1.348973, 12E-2}' file
#Using prinf for fancy output (here spacing)
awk {printf "The name is: %10s ID is %8d\n", $1, $3}' file
#Changing field separator (-F is used to reassign the value of the separator) (here colon is separator)
awk -F: '{ print $1}' file
awk -F"[ :]" '{ print $1, $2}' file
awk -F: '/pattern/'{ print $1, $2}' file
#Print if $3 is less than 5000
awk '$3 < 5000' file
#Print lines starting with an uppercase letter, followed by smaller case letters, followed by space
awk '/^[A-Z][a-z]+ /' file
#Print lines with the patterns
awk '$1 ~ /[Dd]oll/' file
#Print lines without the pattern at the end
awk '$1 !~ /pattern$/' file
POSIX
#Print lines with bracketed characters added by POSIX
awk '$1 ~ /[[:lower:]]+g[[:space:]]+g[[:digit:]]/' file
#Print lines if column $3 has a literal period followed by number between the given range
awk '$3 ~ / \.[2-6]+/' file
#Print $1 and $2 of lines if column $5 does not have the pattern
#Print $1 and $2 of lines if column $5 does not have the pattern
awk '$5 !~ /pattern/{print $1, $2}' file
#Print $1 if line starts with the pattern and then add a string
#Print string and a field $4 if $3 ends with a pattern
#Print $1 if line starts with the pattern and then add a string
awk '$1 ~ /^pattern/{print $1 "How are you?"} file
#Print $4 if ends with three digits
awk '$4 ~ /[0-9][0-9]/{print $4}' file
awk '$3 ~ /pattern$/{print "I paid" $4 "for this book"}' file
#If the line has the pattern, add 5 (subtract 4, divide by 2) to its third column and print the line
COMPARISONS (==, >, =>,<, <=, )
#Print line if $3 is exactly the given value (comparisons)
#If the line has the pattern, add 5 (subtract 4, divide by 2) to its third column and print the line
awk '/pattern/{print $3+ 5}' file
awk '/pattern/{print $3 - 4}' file
awk '/pattern/{print $3 / 2}' file
awk '/pattern/{print $3 - 4}' file
awk '/pattern/{print $3 / 2}' file
COMPARISONS (==, >, =>,<, <=, )
#Print line if $3 is exactly the given value (comparisons)
awk '$3 == 5360' file
awk '$3 > 5000{print $1}' file
awk '$2 <= 23{print $3}' file
awk '!($1 ==10) {print $5}' file
#If $1 is greater than $2, assign $1 to max; If $2 is greater than $1, assign $2 to max; (comparisons)
awk '$3 > 5000{print $1}' file
awk '$2 <= 23{print $3}' file
awk '!($1 ==10) {print $5}' file
#If $1 is greater than $2, assign $1 to max; If $2 is greater than $1, assign $2 to max; (comparisons)
awk '{max=($1 > $2) ? $1 : $2; print max}' file
awk '{print ($5 > 3 ? "High " $5 : "Low " $5)}' file
#Multiplies $3 and $4 and if the result is more than 5000, prints the lines (comparisons)
awk '{print ($5 > 3 ? "High " $5 : "Low " $5)}' file
#Multiplies $3 and $4 and if the result is more than 5000, prints the lines (comparisons)
awk '$3 * $4 > 5000' file
LOGICAL OPEARTORS (&&, ||)
#Prints lines if both conditions are met i.e $2 is more than 10 and less than 20
awk '$2 > 10 && $2 <= 20' file
#Prints lines if both conditions are not met
awk '! ($2 > 10 && $2 <= 20)' file
#Prints lines if any of the conditions are met i.e $2 is more than 10 and less than 20
awk '$2 == 20 || $2 <= 20' file
#Prints lines if any of the conditions are met
awk '$2 == "pattern" || $1 ~ /pattern1/ {print $1, $2}' file
#Prints lines between first occurrence of pattern1 and pattern2 (range patterns)
awk '/pattern1/, /pattern2/' file
awk '/^pattern1/, /^pattern2/' file
# If $2 starts with the specified pattern, add .5 to $5 and print it, also print $3
awk '$2 ~ /^pattern/ \
{print "Percentage: "$5 + .5 "Volume: "$3}' file
LOGICAL OPEARTORS (&&, ||)
#Prints lines if both conditions are met i.e $2 is more than 10 and less than 20
awk '$2 > 10 && $2 <= 20' file
#Prints lines if both conditions are not met
awk '! ($2 > 10 && $2 <= 20)' file
#Prints lines if any of the conditions are met i.e $2 is more than 10 and less than 20
awk '$2 == 20 || $2 <= 20' file
#Prints lines if any of the conditions are met
awk '$2 == "pattern" || $1 ~ /pattern1/ {print $1, $2}' file
#Prints lines between first occurrence of pattern1 and pattern2 (range patterns)
awk '/pattern1/, /pattern2/' file
awk '/^pattern1/, /^pattern2/' file
# If $2 starts with the specified pattern, add .5 to $5 and print it, also print $3
awk '$2 ~ /^pattern/ \
{print "Percentage: "$5 + .5 "Volume: "$3}' file
No comments:
Post a Comment