Giter Site home page Giter Site logo

awk's Introduction

awk

The name "AWK" comes from the initials of its creators: Alfred Aho, Peter Weinberger, and Brian Kernighan

How to Install

AWK is typically included by default in most Unix-like operating systems, including Ubuntu. However, if for any reason it's not installed on your system, you can install it using the package manager. Here's how you can install AWK on Ubuntu:

Open a terminal and type the following command:

sudo apt-get update
sudo apt-get install gawk
gawk --version

Example0

# Ackermann function in AWK
function ackermann(m, n) {
    if (m == 0) {
        return n + 1
    } else if (m > 0 && n == 0) {
        return ackermann(m - 1, 1)
    } else if (m > 0 && n > 0) {
        return ackermann(m - 1, ackermann(m, n - 1))
    }
}

# Example: Calculate A(2, 3)
BEGIN {
    result = ackermann(2, 3)
    print "A(2, 3) =", result
}

awk -f ackermann.awk

Example1

Now, suppose you want to extract and print the names of people who are older than 30. You can use AWK for this task. Here's a simple AWK command:

Name, Age, City
Alice, 28, New York
Bob, 35, San Francisco
Charlie, 22, Los Angeles
David, 40, Chicago

awk -F', ' '$2 > 30 {print $1}' sample.txt

Example2

# Calculate the factorial of a number using recursion
function factorial(n) {
    if (n <= 1) {
        return 1
    } else {
        return n * factorial(n - 1)
    }
}

# Example: Calculate the factorial of 5
BEGIN {
    number = 5
    result = factorial(number)
    print "The factorial of " number " is: " result
}

awk -f factorial.awk

References

https://www.linode.com/docs/guides/introduction-to-awk/

awk's People

Contributors

thinkphp avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.