Use of Linux awk

Source: Internet
Author: User

Awk is a powerful text analysis tool, compared to grep's lookup, the SED editor, when Awk analyzes and generates reports on the data, it appears
particularly powerful. To put it simply, awk reads the file line-by-row, using a space as the default delimiter to slice each row, and the cut part
Analytical Processing.
AWK has 3 different versions: AWK, Nawk, and gawk, which are not specifically described, generally referred to as the GNU version of awk, Gawk,gawk.
Awk has its name from the first letter of its founder Alfred Aho, Peter Weinberger and Brian Kernighan's surname. Real
Awk does have its own language: The AWK programming language, the three-bit creator has formally defined it as "style scan and
Language ". It allows you to create short programs that read input files, sort data, process data, perform a count on input
and generate reports, there are countless other features.
How to use
awk ' {pattern + action} ' {filenames}
Although the operation can be complex, the syntax is always the same, where pattern represents what AWK looks for in the data, and the action is
A series of commands that are executed when a match is found. Curly braces ({}) do not need to always appear in the program, but they are used in accordance with specific
Mode to group a series of instructions. pattern is the regular expression to be represented, surrounded by slashes.
The most basic function of the awk language is to browse and extract information based on the specified rules in a file or string, and awk extracts the information before it can
His text operation. A complete awk script is typically used to format the information in a text file.
Typically, awk is treated as a unit of a file's behavior. awk processes the text by executing the corresponding command for each line that receives the file.

Calling Awk
has three ways to invoke awk:
1. Command-line mode-(emphasis)
awk [-f  field-separator]  ' commands '   input-file (s)
where commands is the true awk command, [-F domain delimiter] is optional. Input-file (s) is the file to be processed.
in awk, each line in a file, separated by a domain delimiter, is called a domain. In general, the default field delimiter is a space without naming the-f field delimited
character.
2.shell Scripting
Inserts all the awk commands into a file and makes the awk program executable, then the awk command interpreter acts as the first line of the script, and
is typically called by typing the script name.
equivalent to the first line of the shell script: #!/bin/bash
can be replaced by: #!/bin/awk
3. Insert all awk commands into a separate file, and then call:
Awk-f awk-script-file Input-file (s)
where the-f option loads the awk script in Awk-script-file, Input-file (s) is the same as above.

Example 1:
Suppose the output of Last-n 5 is as follows:
[[email protected] ~]# last-n 5 <== Remove the first five elements only
Root pts/1 192.168.1.100 Tue Feb 11:21 still logged in
Root PTS/1 192.168.1.100 Tue Feb 10 00:46-02:28 (01:41)
Root PTS/1 192.168.1.100 Mon Feb 9 11:41-18:30 (06:48)
Dmtsai pts/1 192.168.1.100 Mon Feb 9 11:41-11:41 (00:00)
Root tty1 Fri Sep 5 14:09-14:10 (00:01)
If you just show the last 5 accounts logged in:
#last-N 5 | awk ' {print '} ' rootrootrootdmtsairoot
The awk workflow is this: reads a record with a ' \ n ' line break, and then divides the record by the specified field delimiter.
Fills the field, and $ A represents all fields, representing the first field, $n representing the nth field. The default Domain delimiter is "blank key" or "[tab]
The login user, $ $ means the login user IP, and so on

Example 2:
If you just show/etc/passwd's account
#cat/etc/passwd |awk-f ': ' {print $} '
Root
Daemon
Bin
Sys
This is an example of awk+action, where each line executes action{print $.
-f Specifies the domain delimiter as ': '.

If you only display the/ETC/PASSWD account and the shell of the account, and the account and the Shell tab
Cut
#cat/etc/passwd |awk-f ': ' {print $ \ t ' $7} '
Root/bin/bash
Daemon/bin/sh
Bin/bin/sh
Sys/bin/sh

If you only display the/ETC/PASSWD account and the shell of the account, and the account and the shell are separated by commas,
and add the column name Name,shell to all rows and add "Blue,/bin/nosh" to the last line.
CAT/ETC/PASSWD |awk-f ': ' BEGIN {print ' Name,shell '} {print $ ', ' $7}
END {print "Blue,/bin/nosh"} '
Name,shell
Root,/bin/bash
Daemon,/bin/sh
Bin,/bin/sh
Sys,/bin/sh
....
Blue,/bin/nosh
The awk workflow is done by first executing the beging and then reading the file, reading a record with the/n newline character split.
The record is then divided into fields by the specified domain delimiter, fills the field, and $ A represents all fields, representing the first field, $n table
Shows the nth field, followed by the action action corresponding to the execution pattern. And then start reading the second record.
Record ... Until all of the records have been read, and finally the end operation is performed

Example 3:
Search all rows with the root keyword/etc/passwd
#awk-F: '/root/'/etc/passwd
Root:x:0:0:root:/root:/bin/bash
This is an example of the use of pattern, which matches the line of pattern (this is root) before the action is executed (no action is specified,
The default output is the contents of each line).
Search support for the regular, for example, root start: awk-f: '/^root/'/etc/passwd
Search all lines that have the root keyword/etc/passwd and display the corresponding shell
# awk-f: '/root/{print $7} '/etc/passwd
/bin/bash
Action{print $7} is specified here.
awk built-in variables
Awk has many built-in variables for setting up environment information, which can be changed, and some of the most commonly used variables are given below.
ARGC number of command line arguments
ARGV Command line parameter arrangement
ENVIRON support for the use of system environment variables in queues
FileName awk browses the file name
FNR number of records to browse files
FS sets the input domain delimiter, which is equivalent to the command line-F option
NF browsing the number of fields recorded
NR number of records read
OFS output Field delimiter
ORS Output Record delimiter
RS Control record delimiter
In addition, the $ variable refers to the entire record. $ $ represents the first field of the current row, which is the second field of the current row,...... And so on

Example:
Statistics/etc/passwd: File name, line number per line, number of columns per row, corresponding full line contents:
#awk-F ': ' {print ' filename: ' filename ', linenumber: "NR", Columns: "NF
", Linecontent:" $} '/etc/passwd
Filename:/etc/passwd,linenumber:1,columns:7,linecontent:root:x:0:0:root:/root:/bin/b
Ash
Filename:/etc/passwd,linenumber:2,columns:7,linecontent:daemon:x:1:1:daemon:/usr/sbi
N:/bin/sh
Filename:/etc/passwd,linenumber:3,columns:7,linecontent:bin:x:2:2:bin:/bin:/bin/sh
Filename:/etc/passwd,linenumber:4,columns:7,linecontent:sys:x:3:3:sys:/dev:/bin/sh

Use of Linux awk

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.