Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of linux's awk

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how the awk of linux is used, and the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Awk is a powerful text analysis tool, compared with the search of grep and the editor of sed, awk is particularly powerful in analyzing data and generating reports. To put it simply, awk is to read the file line by line, slice each line with a space as the default separator, and then analyze the cut part.

There are three different versions of awk: awk, nawk and gawk, which are not specifically specified. Generally speaking, gawk,gawk is the GNU version of AWK.

Awk gets its name from the initials of its founders Alfred Aho, Peter Weinberger and Brian Kernighan. In fact, AWK does have its own language, the AWK programming language, which the three creators have officially defined as a "style scanning and processing language". It allows you to create short programs that read input files, sort data, process data, perform calculations on input, generate reports, and countless other functions.

Usage

Awk'{pattern + action}'{filenames}

Although the operation can be complex, the syntax is always like this, where pattern represents what AWK looks for in the data, and action is a series of commands that are executed when a match is found. Curly braces ({}) do not need to appear all the time in the program, but they are used to group a series of instructions according to a specific pattern. Pattern is the regular expression that you want to represent, surrounded by diagonal bars.

The most basic function of awk language is to browse and extract information from files or strings based on specified rules. After awk extracts information, other text operations can be carried out. A complete awk script is usually used to format information in a text file.

Typically, awk processes units as a behavior of a file. Awk receives each line of the file and then executes the appropriate command to process the text.

Call awk

There are three ways to call awk

1. Command line mode

Awk [- F field-separator] 'commands' input-file (s)

Where commands is the real awk command, and [- F domain delimiter] is optional. Input-file (s) is the file to be processed.

In awk, each item separated by a domain delimiter in each line of a file is called a field. In general, when the-F domain delimiter is not named, the default field delimiter is a space.

2.shell scripting mode

Insert all the awk commands into a file and make the awk program executable, and then the awk command interpreter, as the first line of the script, is called by typing the name of the script.

Equivalent to the first line of the shell script: #! / bin/sh

Can be replaced by: #! / bin/awk

3. Insert all the awk commands into a separate file and call:

Awk- f awk-script-file input-file (s)

Where the-f option loads the awk script in awk-script-file, and input-file (s) is the same as above.

This chapter focuses on the command line approach.

Getting started example

Suppose the output of last-n 5 is as follows

[root@www ~] # last-n 5

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report