I heard that the awk language can also write scripts

Source: Internet
Author: User

Guide Starting with the Awk series, we're all writing short awk commands and programs in the command line or script file. awk, however, is an interpreted language as well as the shell. With a series of learning from the beginning to the present, you can now write an awk script that can be executed.

Overview

Similar to writing a shell script, the awk script begins with this line:

#! /path/to/awk/utility-f

For example, on my system, the awk tool is installed in the/user/bin/awk directory, so my awk script begins with the following:

#! /usr/bin/awk-f

The above line is interpreted as follows:

    • #! , called the release Companion (), indicating that the interpreter is used to execute the commands in the script;
    • /usr/bin/awk , i.e. interpreter;
    • - f , interpreter option, which specifies the program file to read;

Say so, now let's start with a simple example below, to delve into some of the executable awk scripts. Create a new file using your favorite editor, like this:

$ VI Script.awk

Then paste the following code into the file:

#!/usr/bin/awk-fbegin {printf "%s/n", "Writing my first awk executable script!"}

Exit after saving the file, and then execute the following command to make the script executable:

$ chmod +x Script.awk

Then, execute it:

$./script.awk

Sample output:

Writing My first awk executable script!
Add Comment

A strict programmer will surely ask: "What about the notes?" ”。 Yes, you can include comments in the awk script. Writing comments in code is a good programming habit. It helps other programmers to read your code and understand the functionality of each part of the program file or script. So, you can add comments to the script as follows:

#!/usr/bin/awk-f# This is an example of how to write comments in awk # Use special mode begin to output a sentence begin {printf "%s/n", "Writing my first awk executable script!"}

Let's look at an example of a read file. We want to find a user named Aaronkilik from the account file/etc/passwd, and then print the user name, the user's ID, the user's GID as follows:

Here is the contents of our script file, the file name is Second.awk.

#! /usr/bin/awk-f# uses the begin to specify the character to set the FS built-in variable BEGIN {fs= ":"}# search username Aaronkilik and output account details/aaronkilik/{print "Username:", $, "U Ser ID: ", $ $," User GID: ", $4}

After saving the file, exit, make the script executable, and then execute it as follows:

$ chmod +x second.awk$./second.awk/etc/passwd

Sample output:

Username:aaronkilik User id:1000 User gid:1000

In the last example below, we will use the do while statement to print the number 0-10:

Here is the contents of our script file, the file name is Do.awk.

#! /usr/bin/awk-f#printing from 0-10 using a and Statement#do while Statementbegin {#initialize a counterx=0do {print X ; x+=1;} while (x<=10)}

After saving the file, the script executes as before. Then, run it:

$ chmod +x do.awk$./do.awk

Output sample

012345678910
Summary

We have arrived at the end of this wonderful awk series, and I hope you have learned a lot from past articles, as an introductory guide to your AWK programming language. As mentioned from the beginning, awk is a complete text processing language, so you can learn many other aspects of the AWK programming language, such as environment variables, arrays, functions (built-in or user-defined), and so on. There are other things to learn and master about awk programming, so at the end of the article I provide links to some important online resources that you can use to expand your awk programming skills. But it's not necessary, you can also read some books about awk.

This address: http://www.linuxprobe.com/awk-write-scripts.html

I heard that the awk language can also write scripts

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.