Using awk Operators for Math Functions. Awk uses four arithmetic functions: + addition - subtraction * multiplication / division You can increment and decrement using these operators: ++ Increment -- Decrement Operator position is important in the relation it has to the variable.
If the operator “++” precedes a variable, that variable is incremented before it is used in some other operation. BEGIN {x=5; print ++x} However, if the variable precedes the operator it will print 5.BEGIN {x=5; print x++} There is a shorthand feature which looks like this: Learning awk with Practical Examples. One of the best ways to learn awk is to have a series of commands that you can run to see how the basics work and then build on that.
Here is the first in a series of 10 lessons on how to use awk. Start with creating a file to work with by going to the command line and redirecting the output of the ps command to create a file called processes. Note the processes on your server or desktop will likely be different but the principles are the same. ps aux > processes If you wanted to view each line of the file created use awk without a pattern and the action is to print all lines.awk '{ print }' processes USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.2 2160 660 ? Using Operators with awk Expands Options for Handling Data. In this example the search is for processes that are using 1.0% or more of the system memory.
The operator “>=” is used to find that information. awk '$4 >= 1' processes root 14022 0.0 1.0 10012 2840 ? Ss Feb08 0:00 /usr/sbin/httpd root 15857 0.0 1.1 10032 2900 ? Ss 12:02 0:00 sshd: root@pts/0 In the command you are also able to use comparisons so that you will create a range. In this example the system memory usage needs to be greater than 1% AND less than 1.2%. Using awk with print and printf for Precision. Starting with awk Scripts. Awk scripts allow you to create a script that could be reused and enter a path into a file or execute it from the command line.
When you access the file use the “-f” option to indicate the file. awk -f awk_file In this example a file was created with ps aux > processes so awk could run on that file for searches. The goal was to list lines with “ssh” and “apache”. Managing awk File Structures with the BEGIN and END Sections. The awk file can have three parts which allow you to perform various tasks and enhance your script and it’s usability.
The three sections are; BEGIN, BODY and END. The BEGIN section must have the “BEGIN” text to indicate that it will be used. The BODY actually does not need any reference so that anything you place in the BODY section will execute just as if it was on the command line. Awk Variables in the BEGIN Section. When you need to set variables, you can do that in the BEGIN section of an awk script.
Here the variable firstuser is set to 1000. The BEGIN section sets that variable and it is then used later in the script. awk file: variable BEGIN {firstuser=1000} /1000/ {print; print “UserID of the First User Created: ” firstuser} The search for the text string “1000” does not need to be in single quotes nor does it need to be preceded by awk, since it was already invoked on the command line. Awk -f awkfile /etc/passwd z4:x:1000:1000:z4,,,:/home/z4:/bin/bash UserID of the First User Created: 1000 Total Number of Records: 32 You can also set string variables in the awk script. BEGIN {firstuser=1000; admin="fred"} Awk Field Separators Create Different Output.
Once you start creating awk scripts you will probably want to use them on multiple files and as a result you will require multiple field separators.
Field separators are how awk determines where the fields are. This can be a tricky business because it results in a different output. Counting and Creating Sums with awk. Awk Input and Output Redirection. How To Search Using The Awk Utility. You can do searches with awk by enclosing the search within forward slashes.
Note awk is case sensitive. /Debian/ If you wanted to search for the strings “debian” and “Debian” you could use a regular expression. /[Dd]ebian/ You take those searches and look for the text string at the start of the line, “^” or the end of the line “$” like this: /^Debian/ /Debian$/ If you were looking for lower case letters d-m your search would look like this: /[d-m]/ If you were searching for any number: If you wanted to do searches that matched one text string or another text string you can use a “|” to separate them. This will return both results. Awk ‘$1 ~ /^192.168.5.103$/’ access_log You can negate the search for a string by using “!” To search a series of lines you can use multiple operations. Awk ‘NR == 10,NR == 15′ access_log Here is a list of some of the operators you could use. < Less than. <= Less than or equal. == Equal. ! Various “special” characters are used and can be embedded in strings.