"Go" linux awk command

Source: Internet
Author: User

Introduction

Awk is a powerful text analysis tool, with the search for grep and the editing of SED, which is especially powerful when it comes to analyzing data and generating reports. To put it simply, awk reads the file line-by-row, using spaces as the default delimiter to slice each row, and then perform various analytical processing of the cut. It allows you to create short programs that read input files, sort data, manipulate data, perform calculations on input, and generate reports, as well as countless other features.

How to use
awk ' {pattern + action} ' {filenames} 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. pattern is the regular expression to be represented, surrounded by slashes. 
Call awk
1. Command-line modeawk[-F Field-separator]'commands'input-file(s) wherein, commands is the real 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, without naming-In case of the F field delimiter, the default Domain delimiter is a space. 2The . Shell script inserts all the awk commands into a file and makes the awk program executable, and then the awk command interpreter is invoked as the first line of the script by typing the script name. Equivalent to the first line of the shell script: #!/bin/SHcan be replaced by: #!/bin/awk3Insert all awk commands into a separate file, and then call:awk-Fawk-script-fileinput-file(s) among them,The-f option loads the awk script in Awk-script-file, input-file(s) is the same as above.
Getting Started instance
The awk workflow is this: read in a'\ n'A record for a newline 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] key". #Cat/etc/passwd|awk-F':'  '{print $}'----Press: Divide field, Print 1th column #Cat/etc/passwd|awk-F':'  '{print ' \ t ' $7}'----by: Divide fields, print columns 1th and 7th, tab to split #Cat/etc/passwd|awk-F':'  'BEGIN {print "Name,shell"} {print $ "," $7} END {print "Blue,/bin/nosh"}'----Press: To divide the fields and print the 1th and 7th columns to','and add in the first line"Name,shell", in the last line add"Blue,/bin/nosh"#awk-F':'  '/root/'/etc/passwd----Search/etc/passwd all lines with the root keyword #awk-F':'  '/root/{print $7}'/etc/passwd----Search/etc/passwd All rows with the root keyword and print the 7th column

Variables and Assignments

#awk ' {count++;p rint;} End{print "User Count is", count} '/etc/passwd----statistics/etc/passwd account number
#awk ' BEGIN {count=0;print ' [Start]user count is ', count} {Count=count+1;print $;} End{print "[End]user Count is", count} '/etc/passwd
Conditional statements


Looping statements
#awk-F ': ' BEGIN {count=0;} {Name[count] = $1;count++;}; End{for (i = 0; i < NR; i++) print I, Name[i]} '/etc/passwd
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               command-line arguments argv               command-line parameter arrangement environ            support the use of system environment variables in queues filename           awk browses the file name Fnr the                number of                 records to browse files FS- F option NF                 Browse record number of fields nr                 Read records ofs                output field delimiter ors                Output record delimiter Rs                 control record delimiter
Print and printf
the parameters of the print function can be variables, values, or strings. The string must be quoted in double quotation marks, and the arguments are separated by commas. Using printf instead of print can make your code more concise and easy to read. #awk  ':'  {print ' filename: ' filename ', linenumber: "NR", Columns: "NF", Linecontent: "$ A}' /etc/passwd#awk  ' : '  ' {printf ("filename:%10s,linenumber:%s,columns:%s,linecontent:%s\n", filename,nr,nf,$0)} ' /etc/passwd

Go Linux awk command

Related Article

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.