awk series Part13: How to use awk scripting language

awk series: how to use awk scripting language

awk series Part13: How to use awk scripting language

Awk series from the beginning until the Part 12 , we are all at the command line or a script file to write some short awk commands and programs.

However, like awk and shell is an interpreted language. Through a series of learning now, you can now write awk scripts can be executed from the start.

And almost write shell scripts, awk script to begin with the following line:

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

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

#! /usr/bin/awk -f

Line of the above-explained as follows:

  • #!Called the release with (the Shebang) , indicating the use of the interpreter to execute commands in the script
  • /usr/bin/awk That the interpreter
  • -f Interpreter option to specify the program to read the file

Having said that, from now on the following simple example, let us delve into a few of awk script executable. Use your favorite editor to create a new file, like this:

$ vi script.awk

And paste the code into a file in the following:

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

After you save the file and exit, and then execute the following command, make the script executable:

$ chmod +x script.awk

Then, it:

$ ./script.awk

Sample output:

Writing my first awk executable script!

A strict programmer must ask: "comment it?." Yes, you can include comments in awk script. Write comments in the code is a good programming practice.

It is beneficial to other programmers reading your code, file or program to understand the function of each part of the script.

So, so you can add comments in the script as follows:

#!/usr/bin/awk -f
# 这是如何在 awk 中写注释的示例
# 使用特殊模式 BEGIN 来输出一句话
BEGIN { printf "%s\n","Writing my first awk executable script!" }

Next we see an example of a reading file. We want to find the account file / etc / passwd in a call aaronkilik user, then this print user name like the following, user ID, user's GID (LCTT Annotation: Group ID):

Here are the contents of our script file, the file name second.awk.

#! /usr/bin/awk -f
# 使用 BEGIN 指定字符来设定 FS 内置变量
BEGIN { FS=":" }
# 搜索用户名 aaronkilik 并输出账号细节
/aaronkilik/ { print "Username :",$1,"User ID :",$3,"User GID :",$4 }

After you save the file and exit, make the script executable, and then execute it so like the following:

$ 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 whilestatement to print the numbers 0-10:

Here are the contents of our script file, the file name do.awk.

#! /usr/bin/awk -f
#printing from 0-10 using a do while statement
#do while statement
BEGIN {
#initialize a counter
x=0
do {
print x;
x+=1;
}
while(x<=10)
}

After saving the file, the same as before the operation to make the script executable. Then, run it:

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

Sample Output

0
1
2
3
4
5
6
7
8
9
10

to sum up

We have reached the final of this exciting series of awk, I wish you from the entire 13 chapters learned a lot of knowledge, such as the entry you awk programming language guidance.

I mentioned at the outset, awk is a full text processing language, so you can learn a lot of other aspects of the awk programming language, such as environment variables, arrays, functions (built-in or user-defined), and so on.

There are other awk programming content need to learn and master, so in the end the link I've provided some important online resources, you can use them to expand your awk programming skills. But this is not necessary, you can also read some books on the awk.

If you want to share any thoughts or questions, comments below. Remember to keep our attention, there will be more exciting content.


via: http://www.tecmint.com/write-shell-scripts-in-awk-programming/

Author: Aaron Kili
Translator: Chunyang-wen
proofread: wxy

This article from the LCTT original compiler, Linux China is proud

Reproduced in: https: //blog.51cto.com/wutengfei/2402100

Guess you like

Origin blog.csdn.net/weixin_33720956/article/details/91774792
awk