I have previously plugged the great list of sed 1-liners at http://sed.sourceforge.net/sed1line.txt.
Here is a similar (if shorter) list of handy awk 1-liners:
http://www.sap-basis-abap.com/unix/awk-one-liner-tips.htm:
Print column1, column5 and column7 of a data file or output of any columns list
awk '{print $1, $5, $7}' data_file
cat file_name |awk '{print $1 $5 $7}'
ls –al |awk '{print $1, $5, $7}' -- Prints file_permissions,size and date
List all files names whose file size greater than zero.
ls –al |awk '$5 > 0 {print $9}'
List all files whose file size equal to 512bytes.
ls –al |awk '$5 == 512 {print $9}'
print all lines
awk '{print }' file_name
awk '{print 0}' file_name
Number of lines in a file
awk ' END {print NR}' file_name
Number of columns in each row of a file
awk '{print NF}' file_name
Sort the output of file and eliminate duplicate rows
awk '{print $1, $5, $7}' |sort –u
List all file names whose file size is greater than 512bytes and owner is “oracle”
ls –al |awk '$3 == "oracle" && $5 > 512 {print $9}'
List all file names whose owner could be either “oracle” or “root”
ls –al |awk '$3 == "oracle" || $3 == "root" {print $9}'
list all the files whose owner is not “oracle
ls –al |awk '$3 != "oracle" {print $9}'
List all lines which has at least one or more characters
awk 'NF > 0 {print }' file_name
List all lines longer that 50 characters
awk 'length($0) > 50 {print }' file_name
List first two columns
awk '{print $1, $2}' file_name
Swap first two columns of a file and print
awk '{temp = $1; $1 = $2; $2 = temp; print }' file_name
Replace first column as “ORACLE” in a data file
awk '{$1 = "ORACLE"; print }' data_file
Remove first column values in a data file
awk '{$1 =""; print }' data_file
Calculate total size of a directory in Mb
ls –al |awk '{total +=$5};END {print "Total size: " total/1024/1024 " Mb"}'
Calculate total size of a directory including sub directories in Mb
ls –lR |awk '{total +=$5};END {print "Total size: " total/1024/1024 " Mb"}'
Find largest file in a directory including sub directories
ls –lR |awk '{print $5 "\t" $9}' |sort –n |tail -1
It is very interesting stuff. I ran into something very odd while trying out your examples. It seems that the CSS for section code uses a font-family Courier New. What is odd is that all my browsers display right slanting single quotes with a different font in the CSS code{} section. (gotta love Firebug) With the font that is selected there, I get some left slanting quotes. The issue, as you surely know, is that left slanters have a very different meaning in the shell. Nice blog, thank you for the information.
You are right; I hadn’t noticed that, thanks. I’ll fix it.
I’ve also removed the leading “$” symbols, for better copy/paste
Minor corrections:
List all files names whose file size greater than zero.
ls –al |awk ‘$5 > 0 {print $8}’
List all files whose file size equal to 512bytes.
ls –al |awk ‘$5 == 512 {print $8}’
Good spot!
Excellent list of awk commands. This is inspiring for a beginner like myself.
Hi, good collection of one-liners. Eric Pement has already awk1line.txt and Peteris Krumis explain them in a very detailed way. So take a look here
http://www.catonmat.net/series/awk-one-liners-explained
An easy tip, the default action for a pattern is print $0, so you can make shorter several one-liners, e.g. for print all lines
awk ‘{print 0}’ file_name ==> awk ‘1’ file_name
very very Thanks.these examples are really helpful to me