• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

How to Use user-defined Functions in awk

by admin

User-Defined Functions

You can create user-defined functions in a awk script file using the func or function keywords. Placement in the file is not important; the function definition can occur anywhere in the awk script.

The function can take arguments (local to the function only) or use any existing variable.

The syntax of a function definition is:

func function_name ([optional_arg] . . .) { statements } 

or

function function_name ([optional_arg] . . .) { statements }

The function_name is how the function is called at some other point in the program. Arguments are allowed but not required. In the definition of the function, only the names of the arguments are required. These arguments can be accessed only within the body of the function (the statements inside the curly braces). You might, however, access any other variable that is being used in the awk script from within a function.

For example:

$ cat func.awk 
func MAX(val1, val2) { 
        if (val1 > val2)
	        return val1 
        else 
	        return val2
	                 } 
BEGIN {largest = 0}
{largest = MAX(largest, $5)} 
END {print largest}
$ awk -f func.awk data.file 
5.7

You can use the return statement to return a value from the function. Use this returned value to assign a value to a variable or to perform a test of some sort with an if statement. To perform such a test, the function is called within the if statement. If the function returns a nonzero value, the if statement considers the test to be true; if the function returns a zero value, the if statement considers the test to be false, and the if statements are not executed; for example:

if (function_call()) { statements }

Filed Under: DevOps, Shell Scripting

Some more articles you might also be interested in …

  1. How To Get Information About a Container In Docker
  2. Bash for loop Examples
  3. How to check the status and space used by images and containers
  4. gradle: command not found
  5. Strings and Variables in Python
  6. Shell Script to print pyramid of Stars
  7. Beginners Guide to Using “trap” to Catch Signals and Handle Errors in Shell Script
  8. How to schedule master node running pod/service as a worker node
  9. A Beginner’s Guide To Create Files & Folders Inside Windows Subsystem For Linux
  10. What are Shell Scripts? How to Create Shell Scripts?

You May Also Like

Primary Sidebar

Recent Posts

  • qsub Command Examples in Linux
  • qsub: command not found
  • qrcp Command Examples in Linux
  • qmrestore Command Examples in Linux

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright