0% found this document useful (0 votes)
16 views

Awk Oneliners

Nifty one liners using awk. Learn awk with these useful one liners from an awk master. You life as a system administrator gets a lot better when working with Linux, BSD, and other Unix based systems when you have a strong grasp of how to wield this tool successfully.

Uploaded by

Dan Schmidt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Awk Oneliners

Nifty one liners using awk. Learn awk with these useful one liners from an awk master. You life as a system administrator gets a lot better when working with Linux, BSD, and other Unix based systems when you have a strong grasp of how to wield this tool successfully.

Uploaded by

Dan Schmidt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

AWK One Liners

Command Description
awk '{print FNR "\t" $0}' filename Print line numbers using a tab instead of a space
awk '{ print $NF }' filename Print the last field of each line of the file
awk 'END{print NR}' filename Count the lines in a file. Just like "wc -l".
awk '/Apache/{n++}; END {print n+0}' filename Total the number of lines that contain the name Apache
awk '{ sum += $6 } END { print sum }' filename Add up the numbers of 6th field, print the total
awk '$4 ~ /^[0-9]/ { print $3 }' filename If the 3rd field starts with a number, print that field
awk '{sub(/[ \t]+$/, "");print}' filename Delete the trailing whitespace from end of each line
awk '{gsub(/^[ \t]+|[ \t]+$/,"");print}' filename Delete leading and trailing whitespace from each line
awk -F: '{print $1 " " $2}' filename Print the first 2 fields with a space between the output
awk '{tmp = $1; $1 = $2; $2 = tmp; print}' filename Swap the first 2 fields
awk 'NR <= 5' filename Print the first 5 lines of a file
awk 'END{print}' filename Print the last line of a file
awk '{ $1 = ""; print }' filename Remove the second field in each line and then print it
awk '{sub(/^/, " ");print}' filename Insert 6 blank spaces at beginning of each line
awk '{sub(/^[ \t]+/, ""); print}' filename Delete the leading whitespace (spaces or tabs) from front
Regular Expressions
awk '/regex/' filename Print the lines matching the regular expression
awk '!/regex/' filename Print the lines that don't match the regular expression
awk '/regex/{print x};{x=$0}' filename Print the line before the regular expression match
awk '/regex/{getline; print}' filename Print the line after the regular expression match
awk '/Dog/,/Cat/' filename Print lines between the matches starting one and ending
awk '!/pet/{gsub(/dog/, "cat")};{print}' filename Substitute "dog" with "cat" on lines that don't contain word "pet"
awk 'length < 50' filename Print the lines less than 50 characters
awk 'NR==20,NR==30' filename Print the lines 20 through 30
awk 'NR==50 {print;exit}' filename Print the line 50
Substitution
awk '{gsub(/dog|cat|bird,"pet");print}' filename Find/replace dog,cat or bird with pet and print
awk '{gsub("dog", "cat", $0); print > FILENAME}' *.txt Find/replace dog with cat in every file with extension txt
Counting
awk '/virtual/{n++}; END {print n+0}' filename Print the total number of lines that have the name virtual
ps aux | awk '/program_name/ && !/awk/ {print $2}' > kill Find a program by name from process listing that is not awk and
kill it
awk '{print $7}' logfile | sort | uniq -c | sort -rn | grep "\.jpg" | head Use awk to pull the seventh field of each line of the logfile. Sort
the fields then find the unique fields and count them, do a reverse
sort on numeric count,filter out anything but JPEG files and only
give me the top 10 of that list. unique hostnames or urls from a
logfile
Numbering
awk '{print FNR "\t" $0}' files* Precede each line with number for the line

You might also like