AWK

From WikiMD's Wellness Encyclopedia

  1. AWK

AWK is a powerful programming language designed for text processing and typically used as a data extraction and reporting tool. Named after its creators, Alfred Aho, Peter Weinberger, and Brian Kernighan, AWK is a staple in the Unix programming environment and is widely used for pattern scanning and processing.

History[edit | edit source]

AWK was developed in the 1970s at Bell Labs. It was first released in 1977 and has since become a standard feature of Unix systems. The language was designed to be simple and easy to use, allowing users to write small programs that can perform complex data manipulations.

Features[edit | edit source]

AWK is known for its concise syntax and powerful text processing capabilities. Some of its key features include:

  • **Pattern Matching**: AWK uses regular expressions to search for patterns in text.
  • **Field Processing**: It automatically splits input lines into fields, making it easy to manipulate structured data.
  • **Built-in Functions**: AWK provides a variety of built-in functions for string manipulation, arithmetic operations, and more.
  • **User-defined Functions**: Users can define their own functions to encapsulate reusable code.
  • **Associative Arrays**: AWK supports associative arrays, which allow for flexible data storage and retrieval.

Syntax[edit | edit source]

AWK programs consist of a series of pattern-action statements. Each statement specifies a pattern to search for and an action to perform when the pattern is found. The general form is:

``` pattern { action } ```

For example, the following AWK program prints the second field of each line in a file:

``` awk '{ print $2 }' filename ```

Usage[edit | edit source]

AWK is commonly used for:

  • **Data Extraction**: Extracting specific fields from structured text files.
  • **Data Transformation**: Modifying data formats and structures.
  • **Reporting**: Generating reports from data files.
  • **Scripting**: Automating repetitive text processing tasks.

Variants[edit | edit source]

Several variants of AWK exist, including:

  • **gawk**: The GNU implementation of AWK, which includes additional features and extensions.
  • **nawk**: The "new AWK," which includes improvements over the original version.
  • **mawk**: An AWK implementation focused on speed and efficiency.

Examples[edit | edit source]

Here are some common examples of AWK usage:

  • **Print lines containing a specific pattern**:
 ```
 awk '/pattern/ { print }' filename
 ```
  • **Sum the values in a column**:
 ```
 awk '{ sum += $1 } END { print sum }' filename
 ```
  • **Print the number of lines in a file**:
 ```
 awk 'END { print NR }' filename
 ```

Also see[edit | edit source]


Template:Unix software

WikiMD
Navigation: Wellness - Encyclopedia - Health topics - Disease Index‏‎ - Drugs - World Directory - Gray's Anatomy - Keto diet - Recipes

Search WikiMD

Ad.Tired of being Overweight? Try W8MD's physician weight loss program.
Semaglutide (Ozempic / Wegovy and Tirzepatide (Mounjaro / Zepbound) available.
Advertise on WikiMD

WikiMD's Wellness Encyclopedia

Let Food Be Thy Medicine
Medicine Thy Food - Hippocrates

Medical Disclaimer: WikiMD is not a substitute for professional medical advice. The information on WikiMD is provided as an information resource only, may be incorrect, outdated or misleading, and is not to be used or relied on for any diagnostic or treatment purposes. Please consult your health care provider before making any healthcare decisions or for guidance about a specific medical condition. WikiMD expressly disclaims responsibility, and shall have no liability, for any damages, loss, injury, or liability whatsoever suffered as a result of your reliance on the information contained in this site. By visiting this site you agree to the foregoing terms and conditions, which may from time to time be changed or supplemented by WikiMD. If you do not agree to the foregoing terms and conditions, you should not enter or use this site. See full disclaimer.
Credits:Most images are courtesy of Wikimedia commons, and templates Wikipedia, licensed under CC BY SA or similar.

Contributors: Prab R. Tumpati, MD