The following examples illustrate different ways "awk" can be used to sum columns of numbers in a file. I only have to remove comments that begin with //, might have tabs/spaces before the comment, but not letters. The above will aggregrate the sum of the 1st and 5th columns, but will only print out the sum of the 5th column cat. This is one of the commands that whenever I need to use it for something new and even. In the next block the value of 5th column is added to the sum variable. Compute running average for a column of numbers. 00, but no escape character is used. (columns are separated by spaces by default. bed, where its intervals overlap an interval from input. awk 'BEGIN { FS = ":" } { print $1 | "sort" }' /etc/passwd This program prints a sorted list of the login names of all users. If I just want to add all the number which are in the file (in one column) awk '{x+=$1} END{print x}' file (I can just which column I sum by changing $1 to $2 as usual. Sum column in Linux is very easy and you can do it by a single command. Let us print these two columns using AWK print command. txt) which contains the below data. Awk has built in string functions and associative arrays. Joining two files by columns. Division; because all numbers in awk are floating-point numbers, the result is not rounded to an integer—‘3 / 4’ has the value 0. AWK also has string manipulation functions, so it can search for particular strings and modify the output. Usually, all books on unix mention this command, but only some of them treat it in detail. Many utility tools exist in the Linux operating system to search and generate a report from text data or file. , and the whole line as $0. count occurences of each word in novel David Copperfield. They are all shown for the print statement, but they work identically for printf also. awk 'BEGIN{sum=0}{sum=sum+ $2 }END{print sum}' test. I put together a short intro presentation for some people explaining a little bit about R from an introductory point of view. Using this command we can find out the total of all numbers in the specific column. Create a file sum_column and paste the below script in that file #!/usr/bin/awk -f BEGIN {sum=0} {sum=sum+$5} END {print sum. bed, where its intervals overlap an interval from input. Command line column sum 27 February 2017 unix Adventures in /usr/bin and the likes , a blog post by Alexander Blagoev, is full of all sorts of very useful short examples of Unix/Linux command line tools. You can change this by using the -F option to awk and supplying another character. In the Begin block the variable sum is assigned with value 0. I have an input file (num. AWK: Sum columns, write them into a “final” row. Eg if we consider the first column than we can do the. Here are some examples of how awk works in this use case. Likewise it keeps adding the value of 5th column when awk see's the record which satisfies the given condition. csv | awk -f test. If you would like to sum the third column the command will be awk 'BEGIN{sum=0}{sum=sum+$3}END{print sum}' test. awk '$1 !~ /^#/ && $0 ~ /[^ ]/ {print $2+$3+$4,"\t",$1}' filename. And hence the first column is accessible using $1, second using $2, etc. A protip by revathskumar about awk. it has a column of floating point data whose length and scale can be long. awk - sum total if field = string hi i got this file and i want to add to the total if [ $5 = "M" ] each row has a variable number of bytes so using an index doesnt add everything up correctly:. Date: May 21, 2005 The awk* command is a powerful tool for extracting and manipulating data from a file. 111111 3,1201,0. Regardless of the separator used, awk makes it easy to extract just what you need. In awk, $0 is the whole line of arguments. bed print out only the header and the entry for colorado in states. Now as awk reads its input line by line it increments sum by the value in column 5(as specified by $5). The input is piped through the expand program to change tabs into spaces, so the widths compared are actually the right-margin columns. In this example we will see how to execute the awk script written in a file. The --echo option reports each interval from input. While awk is awesome — I post lots of answers that use it — there is a better toolkit for this. A long time later, we are back in my life again. keep lines in a file if a certain field repeats at least N times. Here's the final result: a1 c1 a2 c2 a3 c3. Your articles will feature various GNU/Linux configuration tutorials and FLOSS technologies used in combination with GNU/Linux operating system. Date: May 21, 2005 The awk* command is a powerful tool for extracting and manipulating data from a file. To find out more, including how to control cookies, see here. txt has the successes and attempts in columns 2 and 3, respectively, we could do this:. I need exact result such as 385858. While some awk programs are complicated enough to require a separate file containing the program, you can do some amazing things using an argument on the command-line. 192387 2,1201,0. When all the rows are processed the sum variable will hold the sum of the values in the 5th column. Printing Fields and Searching. awk numbers. com The awk is a powerful Linux command line tool, that can process the input data as columns. The awk language supports the set of built-in variables described in Section 2. Again text file data. Each matrix is of 3×3. This tries to get the sum of all answers where question 1 is the same, not the sum of the answers for question 2 where question 1 is the same (based on participation_id?). For some reason an errant CRLF is being inserted in the file after every 23,036 characters, causing all sorts of problems. 1) If the 1st column (i. Now I just run both through awk like this: awk -f test. The word awk is actually derived from the names of its three developers A lfred Aho, Peter W einberger, and Brian K ernighan. The sequential order of the output is indeterminate, as is usual with associative array. # Here's a quick example of a simple script, the sort of thing AWK is perfect # for. To find the sum of all entries in second column and add it as the last record. While some awk programs are complicated enough to require a separate file containing the program, you can do some amazing things using an argument on the command-line. Finally here are a few of the kinds of tasks that I do with AWK all the time 1) Convert some file with 10 or more columns into one with a sum of a few and reformating the others: >awk '{print toupper($1) "," ($3/100) "," ($2+$4-$5)}' FILENAME 2) Calculate the Mean and Standard Deviation on a column. Create a file sum_column and paste the below script in that file #!/usr/bin/awk -f BEGIN {sum=0} {sum=sum+$5} END {print sum. Assigning to $0 shall reset the values of all other fields and the NF built-in variable. $ numsum -r columns 55 60 65 70 75. Sum of Rows in column using awk. Audience: All. I have an input file (num. How to print all coloumns using AWK. --- awk 'NF > 1 {print $0}' list awk -F: 'NF != 7 {print $0 }' /etc/passwd # entry that does not have 7 columns will be printed for review -F: use colon as field/column delimiter NF = number of fields (ie, number of columns) $1 = first column $2 = second column $0 = whole line # perl column numbering starts with 0 # python column numbering. Display the car make and type for each car whose 5th column is in the range of $2000 to $4000 inclusive Display the records from the first plymouth to the first ford Display the records from the cars file where column 2 has four or more letters in it. Ask Question Asked 5 years, 3 months ago. And we won't even need to read the entire file into memory. I have a homework project that asks me to average a column in a spreadsheet. txt This will select the element from column 3 of each line if the columns are considered to be separated by a comma. Is there a way to do this ? Sample data file: Car1 2019/04/03 150100 150105 Store Car 2 2019/04/04 150210 150239 Gym. shyam_rt84 used Ask the Experts™ on 2016-12-21. man awk contains complete documentation of options, and to get the most out of awk (or any Unix/Linux command) you need a good grasp of regular expressions, and for this I recommend the wonderful book Mastering Regular Expressions. The file is in a windows format, all line end. 1) If the 1st column (i. Is it possible to use awk (or other linux tools) to sum columns only when they match a condition? For example, I have 2 cars I use for business travel. awk '{ nlines++ } END { print nlines }' This programs counts lines in a file. The sequential order of the output is indeterminate, as is usual with associative array. When all the rows are processed the sum variable will hold the sum of the values in the 5th column. For example, the following is a complete AWK program that reads an entire file from the standard input device, splits each file into whitespace-separated columns, and outputs all lines in which the fifth column is an odd number:. Or we can say that awk is mainly used for grouping of data based on either a column or field, or on a set of columns. I only have to remove comments that begin with //, might have tabs/spaces before the comment, but not letters. In AWK, the first field is referred to as $1, the second as $2 and so on. Awk has been included on all Unix systems since 7th Edition Unix in 1979. tab with illiteracy rate less than 0. This value is printed in the End block. $ awk -F"," '{x+=$2;print}END{print "Total,"x}' file Item1,200 Item2,500 Item3,900 Item2,800 Item1,600 Total,3000 This is same as the first example except that along with adding the value every time, every record is also printed, and at the end, the "Total" record is. I put together a short intro presentation for some people explaining a little bit about R from an introductory point of view. I downloaded one of the gzipped tsv, then unzipped it using gzip , piped that to awk. awkseeker said I used two of your tricks and perfect work for me. I have been receiving a text file where each row should be 246 columns in length. SED: stream oriented editor sed 'instructions' filename command | sed 'edit-instructions' sed -f command. Awk is an excellent tool for reading in and processing structured data, such as the system's /etc/passwd file. AWK Stands for 'Aho, Weinberger, and Kernighan' Awk is a scripting language which is used for processing or analyzing text files. So, I could call awk 6 times, but I *know* that ain't how it's done in awk-land. In the Begin block the variable sum is assigned with value 0. txt), PDF File (. If the column separator is something other than spaces or tabs, such as a comma, you can specify that in the awk statement as follows: awk -F, '{ print $3 }' table1. extracting columns from multiple files with awk hi everyone! I'd like to extract a single column from 5 different files and put them together in an output file. The AWK command sums the fifth column (filesize) of the ls -l (bytes). If the column separator is something other than spaces or tabs, such as a comma, you can specify that in the awk statement as follows: awk -F, '{ print $3 }' table1. I've found awk code to total any one column, but not the totals of all the columns. (This works because awk variables are automatically initialized to zero. $ numsum -c -x 1,2,5 columns 15 40 115 Add up the rows of numbers of a file. Paragraph mode. The following examples illustrate different ways "awk" can be used to sum columns of numbers in a file. Using variables in Awk will feel different than in other languages. When all the rows are processed the sum variable will hold the sum of the values in the 5th column. Printing All Lines. In the way of you would like to print multiple columns mention the column names separated by comma $1,$2,$3…. Using AWK to sum column from different files. Scripting Languages; AWK; How to sum the values in column,Rows using awk x,y,8,0,0. By continuing to use this website, you agree to their use. You can use it for matrix of any degree and it is not neessary that all the matrices will be of same degree 1. 423424234 sdasdfdas|23214234142423432432. > for > while > count=$((count += next_value)) > test > until > If you use external commands there's awk, xargs and even bc. bed, while the --sum operation sums the score data in the fifth column of ref. awk - sum rows. That’s life without AWK. "Hi all I have a file that has 4 columns. I am using awk to grep 'foo' from a text file and cacluate sum of field # 7. For example, the following is a complete AWK program that reads an entire file from the standard input device, splits each file into whitespace-separated columns, and outputs all lines in which the fifth column is an odd number:. UnixDude "To be a warrior is not a simple matter of wishing to be one. 9/4/12& 2& The&structure&of&an&awk&program& line&oriented& & 1. It adds up the first column, and prints the sum and average: { s += $1 } END { print "sum is", s, " average is", s/NR }. All variables are initialized to the numeric value of 0 (zero. 111111 3,1201,0. And hence the above command prints all the names which happens to be first column in the file. Then you have to make the sum with awk. AWK has built in string functions and associative arrays and supports all functionalities of C language. You get the sum of all the numbers in a column: awk '{total += $1} END {print total}' earnings. Note that there is no pattern specified here so awk will do the action for every line. com The awk is a powerful Linux command line tool, that can process the input data as columns. first one can be of 3×3, second one can be of 3×4 and so on. We can also convert AWK scripts into Perl scripts using a2p utility, an added advantage of AWK. Reading Input Files. txt has the successes and attempts in columns 2 and 3, respectively, we could do this:. Scripting Languages; AWK; How to sum the values in column,Rows using awk x,y,8,0,0. % awk '{x1 = substr($0, 48, 9); printf("%s ",x1)}' infile will list columns 48-56 on stdout. All the above awk command is doing is accepting the input of "this. In this article, I show some examples of these one liners. (The variable $0 contains the entire line. The above will aggregrate the sum of the 1st and 5th columns, but will only print out the sum of the 5th column cat. Here are some awk examples to sum an integer column from a file or stdin (standard input) stream. awk is not just a command. Compute running average for a column of numbers. The above command executes the awk program in prog. Using variables in Awk will feel different than in other languages. To find out more, including how to control cookies, see here. Likewise it keeps adding the value of 5th column when awk see's the record which satisfies the given condition. Each matrix is of 3×3. 423432 in unix, i tried awk but it is rounding off large values. As a result, when awk has finished reading all the input lines, sum is the total of the sizes of the files whose lines matched the pattern. It can be considered to be a pseudo-C interpretor, as it understands the same arithmatic operators as C. It finds the sum of a particular column, you use it as a filter, ie: pipe something into it and it gives you an output. Had to search this one for a while… here is the. I'd like to be able to email out the following:. This is a personal weblog. To find out more, including how to control cookies, see here. This adds the fifth field (the file's size) to the variable sum. Awk works best on files that have columns of numbers or strings that are separated by whitespace (tabs or spaces), though on most machines you can use the -F option if your columns are set apart by another character. Many utility tools exist in the Linux operating system to search and generate a report from text data or file. Unless there is an AIX command which I haven't stumbled across yet, you need to work all this out by doing the following. awk, computes the sum and average of the numbers in the second column of the input file, chapter1: { sum += $2 } END { print "Sum: ", sum; print "Average:", sum/NR; } The first action adds the value of the second field of each line to the variable sum. In this post we will cover printf statement which very much use full for formatting output as the padding spaces before and after the column entries etc. The awk command programming language requires no compiling, and allows the user to use variables, numeric functions, string functions, and logical operators. They are all shown for the print statement, but they work identically for printf also. Then you have to make the sum with awk. AWK: Print Column – Change Field Separator – Linux Bash Posted on Tuesday December 27th, 2016 Thursday August 17th, 2017 by admin The awk is a powerful Linux command line tool, that can process the input data as columns. b) the average i. Built-in functions are functions that are always available for your awk program to call. Let us print these two columns using AWK print command. txt The $2 actually defines which column you would like to sum. This is a common problem. How do I do that. I recently published a library for advanced awk-like file manipulation in Python 3. While some awk programs are complicated enough to require a separate file containing the program, you can do some amazing things using an argument on the command-line. Find min max of a column using awk - bash I already discussed how to find min, max and average using awk in one of my earlier post titled Min,max and average using awk Here is a simple one liner using awk to find the min and max of a column. In the script, the sales array will store all values of the second field and sum variable is initialized with 0 to add all values of sales array. If you specify input files, awk reads them in order. How to print all coloumns using AWK. the pattern /foo/ matches any input record containing the three characters 'foo', *anywhere* in the record. Data used in these examples: read more. extracting columns from multiple files with awk hi everyone! I'd like to extract a single column from 5 different files and put them together in an output file. We pipe this to uniq because the default behavior will print the number of columns for each row and since each row has the same number of columns, uniq will reduce this to one number. I extract the sum and average of column number 1. Date: May 21, 2005 The awk* command is a powerful tool for extracting and manipulating data from a file. awk - sum total if field = string hi i got this file and i want to add to the total if [ $5 = "M" ] each row has a variable number of bytes so using an index doesnt add everything up correctly:. awk 'BEGIN{system("dir")}'. Again text file data. Compute running average for a column of numbers. 00, but no escape character is used. This addition of the 5th column to the sum variable repeats for every row it processed. bed, while the --sum operation sums the score data in the fifth column of ref. In the following note i will show how to print columns by numbers – first, second, last, multiple columns etc. The syntax of awk can also take a little getting used to. Of course, your data will most probably will be in a less readable form, so you'll have to do a bit of parsing before you get to sum the numbers up, but the loop will be organized in the same way. # Here's a quick example of a simple script, the sort of thing AWK is perfect # for. I am hoping someone can help me find a way to sum two columns but output the answer into a separate file using the first row of the original file as such: 00002 Sum multiple columns output to single row to in separate file. ) cat myFile | awk '{print $7}' For delimiter other than space, for example tab, use -F option. --- awk 'NF > 1 {print $0}' list awk -F: 'NF != 7 {print $0 }' /etc/passwd # entry that does not have 7 columns will be printed for review -F: use colon as field/column delimiter NF = number of fields (ie, number of columns) $1 = first column $2 = second column $0 = whole line # perl column numbering starts with 0 # python column numbering. Using variables in Awk will feel different than in other languages. Write an Awk program that finds the average weight of all coins minted in the USA. Command line column sum 27 February 2017 unix Adventures in /usr/bin and the likes , a blog post by Alexander Blagoev, is full of all sorts of very useful short examples of Unix/Linux command line tools. In the next block the value of 5th column is added to the sum variable. While awk is awesome — I post lots of answers that use it — there is a better toolkit for this. $ numsum -c -x 1,2,5 columns 15 40 115 Add up the rows of numbers of a file. Or like this (if you were using cat, which isn't necessary): cat test. Looking at Amazon, we find more titles such as Effective Awk Programming: A User's Guide, oriented to gawk, and half a dozen titles more. Is it possible to use awk (or other linux tools) to sum columns only when they match a condition? For example, I have 2 cars I use for business travel. All variables are initialized to the numeric value of 0 (zero. It adds up the first column, and prints the sum and average: { s += $1 } END { print "sum is", s, " average is", s/NR }. All you have to do is write the patterns and actions # based on what you expect of the input, and what you want to do with it. below is the output which as 3rd column in all the rows [[email protected] awk]# awk '{ print $3 }' test. Fortunately, it is very easy. deleting the columns doesn’t seem to be necessary since you’re not printing the resulting panel. I use awk all the time, but generally only to conveniently pull a particular field out of data that I'm workin with. If the column separator is something other than spaces or tabs, such as a comma, you can specify that in the awk statement as follows: awk -F, '{ print $3 }' table1. Ask Question Asked 5 years, 3 months ago. the pattern /foo/ matches any input record containing the three characters 'foo', *anywhere* in the record. When all the rows are processed the sum variable will hold the sum of the values in the 5th column. I hope this is somewhat clear, this wasn't an easy one to explain (or figure out). This is a more straightforward one. Tcl has no separate Boolean type. Using awk, how can we change the names of all of these files so that the EHZ or HHZ is replaced by just Z. Create a file sum_column and paste the below script in that file #!/usr/bin/awk -f BEGIN {sum=0} {sum=sum+$5} END {print sum. Using variables in Awk will feel different than in other languages. When the end of file is reached, the second part of the command is executed, which just prints the value of the variable. Your goal requires two passes of the file: one to calculate the column sum, and a second to print out the entire column (for the appropriate columns). Reading Input Files. How to Perform Group by Based on Single Column using AWK Input Data : F_Data_File. I tried the command below, but apparently I'm doing something Awk: sum on a column range - AWK - Tek-Tips. I have 3 columns and would like to get the average of 3 of them. Create a file sum_column and paste the below script in that file #!/usr/bin/awk -f BEGIN {sum=0} {sum=sum+$5} END {print sum. sed and awk are pretty bad at handling dates because they generally treat them. To find out more, including how to control cookies, see here. ) First, you have to cut the 15th column. It displays the unique values in the 2nd column. Sponsored by #native_company# — Learn More. $ awk -F"," '{x+=$2;print}END{print "Total,"x}' file Item1,200 Item2,500 Item3,900 Item2,800 Item1,600 Total,3000 This is same as the first example except that along with adding the value every time, every record is also printed, and at the end, the "Total" record is. Add all the values together to get the total sum. awk 'BEGIN{system("dir")}'. In this guide, we will discuss the basics of how to use awk to process text files efficiently. We would be using awk command to count the number of columns in each row in a text file. 9/4/12& 2& The&structure&of&an&awk&program& line&oriented& & 1. If you would like to sum the third column the command will be awk 'BEGIN{sum=0}{sum=sum+$3}END{print sum}' test. com The awk is a powerful Linux command line tool, that can process the input data as columns. Example:6 How to find the total of all numbers in a column. But, result is rounded to an integer. Sum a column of file sizes output from an list (ls) command using awk. $ awk '{ SUM+=$1 } END { print SUM }' < nums That SUM+= operation adds the contents of column one to a running total for each line of input. # Print first two fields in opposite order:awk '{ print $2, $1 }' file# Print lines longer than 72 characters:awk 'length > 72′ file# Print length Kroko Just another WordPress weblog March 4, 2008. Fortunately, it is very easy. The above will print out the sum of the 1st column and redirect the otuput to a file. (You can also define new functions yourself. A simple awk command can be run from a single command line. I use awk all the time, but generally only to conveniently pull a particular field out of data that I'm workin with. The facts presented are collected by refering various websites, books or personal experiences. Printing All Lines. [ up to 38 ]. csv files, with some numbers, I want to get a new file with the same number of rows and one column, with the sum of the numbers of the corresponding row in the original. A colleague of mine used AWK to extract the first column from a file with the command: awk ' '{print $1}' file Easy, isn't it? This simple task does not need complex programming in C. Here’s the whole command: cut -d',' -f15 flightdelays. awk '{ nlines++ } END { print nlines }' This programs counts lines in a file. txt For more examples on awk command: Examples of awk command - part 1 Examples of awk command - part 2 If you like this post, then please recommend it on google by clicking on the +1 button. Tagged: awk, linux. Using AWK to sum column from different files. AWK is an excellent tool for processing these rows and columns, and is easier to use AWK than most conventional programming languages. If you want to only print the x row $ awk 'NR == 2' file second $ awk 'NR == 5' file fifth The below example prints nothing. The following program, sum2. Note that awk may have functions that you are looking for and you wouldn't need bc. A shell script to list all IP addresses that accessing your website. Or like this (if you were using cat, which isn't necessary): cat test. A long time later, we are back in my life again. Finding the sum or average of a column of data. below is the output which as 3rd column in all the rows [[email protected] awk]# awk '{ print $3 }' test. Likewise it keeps adding the value of 5th column when awk see's the record which satisfies the given condition. Sponsored by #native_company# — Learn More. i need to calculate the sum of this column. AWK Command : Session 3 : Select columns from CSV file. I am using awk to grep 'foo' from a text file and cacluate sum of field # 7. Printing All Lines. how to count number of rows and sum of column using awk Hi All, I have the following input which i want to process using AWK. awk '{print $1}' : this is where you pick which column you want $1 is the first $2 is the second. If&there&is&a&match&process&the&line&and&produce&a&new&. Let’s say you want to sum up all the ArrDelays (like you would do in Excel with the SUM command. Each matrix is stored in a file: 1 row in every line and columns are space separated. awk numbers. awk programs are pattern-action pairs. i used bc but that is very slow for a large file. In the next chapter, we learn how to write Awk programs that are longer than one line. AWK command is one of the most powerful tools in Unix used for processing the rows and columns in a file. AWK Book Code — contains all the programs from The AWK Programming Language, by Aho, Kernighan and Weinberger (Addison-Wesley, 1988). txt > output1. I can't figure out the command to do if. The following program, sum2. $ awk -F"," '{x+=$2;print}END{print "Total,"x}' file Item1,200 Item2,500 Item3,900 Item2,800 Item1,600 Total,3000 This is same as the first example except that along with adding the value every time, every record is also printed, and at the end, the "Total" record is. As a result, when awk has finished reading all the input lines, sum is the total of the sizes of the files whose lines matched the pattern. When all the rows are processed the sum variable will hold the sum of the values in the 5th column. To find out more, including how to control cookies, see here. You can use it for matrix of any degree and it is not neessary that all the matrices will be of same degree 1. (It is a common mistake, especially for C programmers, to forget that all numbers in awk are floating point, and that division of integer-looking constants produces a real number, not an integer. txt has the successes and attempts in columns 2 and 3, respectively, we could do this: If the file data. , apply same change to lots of source files Filter, i. The output should look like: >> Sum is: X; Average is: X. This value is printed in the End block. We can also use AWK to select and print parts of the file. 1? greater than 2?. NF is an AWK built in variable and it stands for number of fields. bed, while the --sum operation sums the score data in the fifth column of ref. txt print columns separated by tab change output field separator (OFS) to tabular "\t", instead of using space by default. I have 3 columns and would like to get the average of 3 of them. Create a file sum_column and paste the below script in that file #!/usr/bin/awk -f BEGIN {sum=0} {sum=sum+$5} END {print sum. In the next chapter, we learn how to write Awk programs that are longer than one line. When the end of file is reached, the second part of the command is executed, which just prints the value of the variable. This adds the fifth field (the file's size) to the variable sum. of Computer Science Columbia University sed: string replacement Line-oriented tool for pattern matching and replacement (stream editor) Not really a programming language (cf. When all the rows are processed the sum variable will hold the sum. This is one of the commands that whenever I need to use it for something new and even. csv | awk -f test. You can use it for matrix of any degree and it is not neessary that all the matrices will be of same degree 1. For sum(12+13+…+20) it’s 10 more for each element, i. For example, the following is a complete AWK program that reads an entire file from the standard input device, splits each file into whitespace-separated columns, and outputs all lines in which the fifth column is an odd number:. In the Begin block the variable sum is assigned with value 0. $ awk 'func sum (s). of Computer Science Columbia University sed: string replacement Line-oriented tool for pattern matching and replacement (stream editor) Not really a programming language (cf. I recently published a library for advanced awk-like file manipulation in Python 3. This is our third post on AWK scripting. awk, computes the sum and average of the numbers in the second column of the input file, chapter1: { sum += $2 } END { print "Sum: ", sum; print "Average:", sum/NR; } The first action adds the value of the second field of each line to the variable sum. When awk has completed reading the file it prints out the sum. dat with columns of numbers. i need to calculate the sum of this column. cut will do that. I realized the awesome power of 'awk' when I had to perform numerical operations on some of the columns of the 32 column 2000 line data file. awk, a stream programming language, ctd. ls -lh php*; ls -l php* | awk '{ SUM += $5} END { print SUM/1024/1024 }' The line above prints the list with 'human' numbers ls -lh for all files that start with php. And we won't even need to read the entire file into memory.