Awk Case Study

Source: Internet
Author: User

Awk is a powerful text analysis tool.Line by lineSlice each line with spaces as the default separator, and then perform various analysis. The most basic function of the awk language is to browse and extract information based on specified rules in a file or string. Only after awk extracts information can other text operations be performed.

Usage

awk ‘{pattern+action} {filename}‘

Pattern indicates the content that awk looks for in the data, and action is a series of commands executed when matching content is found.

Example 1: Read the content of a specified column.(Last-N: list the information of N users who have logged on to the system recently)

[[email protected]~]$last -5jihite   pts/4        :0.0             Mon Aug 18 23:00   still logged in   jihite   pts/2        :0.0             Mon Aug 18 23:00   still logged in   jihite   pts/1        :0.0             Mon Aug 18 23:00   still logged in   reboot   system boot  3.2.0-61-generic Mon Aug 18 22:40 - 23:03  (00:23)    reboot   system boot  3.2.0-61-generic Sat Aug 16 20:27 - 12:54  (16:26)    wtmp begins Sat Aug  2 01:18:13 2014

Now we can use the awk command to extract only the username in the first column

[[email protected]~]$last -5 | awk ‘{print $1}‘jihitejihitejihiterebootrebootwtmp

Awk workflow: Read data row by row (separated by '\ n'). It is separated by spaces by default. $0 indicates the content of the entire row and $1 indicates the content of the first column.

If you only display the/etc/passwd account

#cat /etc/passwd |awk  -F ‘:‘  ‘{print $1}‘  rootdaemonbinsys

This is an example of awk + action. Action {print $1} is executed on each line }.

-F specifies that the domain separator is ':' (the default domain separator is space ).

Example 2:-F specifies the Separator

If only the/etc/passwd account and shell corresponding to the account are displayed, the account and shell are separated by the tab key.

#cat /etc/passwd |awk  -F ‘:‘  ‘{print $1"\t"$7}‘root    /bin/bashdaemon  /bin/shbin     /bin/shsys     /bin/sh

Separated by \ t between $1 and $7

Example 3: add an output at the beginning and end of the output

Add the name and shell columns to all rows, and add "Blue,/bin/nosh" to the last row ".

cat /etc/passwd |awk  -F ‘:‘  ‘BEGIN {print "name,shell"}  {print $1","$7} END {print "blue,/bin/nosh"}‘name,shellroot,/bin/bashdaemon,/bin/shbin,/bin/shsys,/bin/sh....blue,/bin/nosh

The awk workflow is as follows: first execute beging, then read the file, read a record with/N line breaks, then divide the record into Domains Based on the specified domain separator, and fill in the domain, then start the action corresponding to the execution mode. Then read the second record until all the records are read and the end operation is executed.

Example 4: Search for rows containing a keyword(Find the row containing the keyword 'man)

[[email protected]~]$cat ‘/etc/passwd‘ | awk -F : ‘/man/{print $0}‘ man:x:6:12:man:/var/cache/man:/bin/shcolord:x:102:107:colord colour management daemon,,,:/var/lib/colord:/bin/false

Example 5: built-in Variables

Argc command line parameter count argv command line parameter arrangement environ supports the number of records in the FNR file browsed by filename awk for system environment variables in the queue. FS sets the input domain separator, it is equivalent to the number of domains in the command line-F option NF browsing record Nr number of records read OFS output domain separator ors output record separator Rs control record Separator

Instance

[[email protected]~]$awk  -F ‘:‘  ‘{print "filename:" FILENAME ",linenumber:" NR ",columns:" NF}‘ /etc/passwdfilename:/etc/passwd,linenumber:1,columns:7filename:/etc/passwd,linenumber:2,columns:7filename:/etc/passwd,linenumber:3,columns:7filename:/etc/passwd,linenumber:4,columns:7filename:/etc/passwd,linenumber:5,columns:7filename:/etc/passwd,linenumber:6,columns:7filename:/etc/passwd,linenumber:7,columns:7filename:/etc/passwd,linenumber:8,columns:7filename:/etc/passwd,linenumber:9,columns:7filename:/etc/passwd,linenumber:10,columns:7filename:/etc/passwd,linenumber:11,columns:7filename:/etc/passwd,linenumber:12,columns:7filename:/etc/passwd,linenumber:13,columns:7filename:/etc/passwd,linenumber:14,columns:7filename:/etc/passwd,linenumber:15,columns:7filename:/etc/passwd,linenumber:16,columns:7filename:/etc/passwd,linenumber:17,columns:7filename:/etc/passwd,linenumber:18,columns:7filename:/etc/passwd,linenumber:19,columns:7filename:/etc/passwd,linenumber:20,columns:7filename:/etc/passwd,linenumber:21,columns:7filename:/etc/passwd,linenumber:22,columns:7filename:/etc/passwd,linenumber:23,columns:7filename:/etc/passwd,linenumber:24,columns:7filename:/etc/passwd,linenumber:25,columns:7filename:/etc/passwd,linenumber:26,columns:7filename:/etc/passwd,linenumber:27,columns:7filename:/etc/passwd,linenumber:28,columns:7filename:/etc/passwd,linenumber:29,columns:7filename:/etc/passwd,linenumber:30,columns:7filename:/etc/passwd,linenumber:31,columns:7filename:/etc/passwd,linenumber:32,columns:7filename:/etc/passwd,linenumber:33,columns:7filename:/etc/passwd,linenumber:34,columns:7filename:/etc/passwd,linenumber:35,columns:7filename:/etc/passwd,linenumber:36,columns:7

Example 5: awk Programming

[[email protected]~]$awk ‘{count++;} END{print "user count is:" count}‘ /etc/passwduser count is:36

 

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.