Awk control function, awk Function
Example$ Cat datafile
Northwest NW Joel Craig 3.0. 98 3 4
Western WE Sharon Kelly 5.3. 97 5 23
Southwest SW Chris Foster 2.7. 8 2 18
Southern SO May Chin 5.1. 95 4 15
Southeast SE Derek Johnson 4.0. 7 4 17
Eastern EA Suan Beal 4.4. 84 5 20
Northeast ne tj Nicholas 5.1. 94 3 13
North NO Val Shultz 4.5. 89 5 9
Central CT Sheri Watson 5.7. 94 5 13
$
Printf- Align LeftWidth Field step, 0 for 0 step. Prec The maximum string length, or the number of digits to the right of the decimal%c ASCII characters%d integral type%e Scientific Counting method%f floating point type%g awk determines which floating-point number is converted to E or F%o octal%s string%x HexExample one:i161 Admin # df-th|awk ' {print $1,$5} 'Filesystem AvailRootfs 23G/dev/root 23GTmpfs 16G
Use the awk command to merge the two lines with the same name in the following two files.[[email protected] ~]# cat 1.txt Han Hailin 21-year-old Hailin Han 23-year-old Han Linhai 22-year-old Lin Han 24-year-old [[email protected] ~]# cat 2.txt Han Linhai male hailin Han Nan Han Hailin male Linhai Han NanOutput effect:Han Hailin 21-year-old male[[email protected] ~]# awk '
Awk command usage
Awk Programming Language/Data Processing Engine
Creator: Aho weinberger kernighan
Check input based on pattern matching (read row-by-row output)
Print the expected matching result to the screen
Syntax format:
Awk 'mode {operation}' file 1 file 2 .....
Common built-in Variables
The ordinal number of the row being processed by
I. Directly process awk COMMANDS IN SHELL
Awk '{print $0}' file
Awk '/^ (11 + ?) \ 1 + $/{print $0} 'file // The famous regular that determines whether it is a prime number
The preceding two commands are commands that can be directly executed in shell. The basic mode is awk pattern {action} file, which means that
/passwdOFS: Output field delimiter, default to white space characterAwk-v fs= ': '-v ofs= ': ' {print $1,$3,$7} '/etc/passwdRS: Enter the record delimiter, specify the line break at input, and the original newline character is still validAwk-v rs= ' {print} '/etc/passwdORS: Output record delimiter, output with specified symbol instead of line breakAwk-v rs= "-v ors= ' # # # ' {print} '/etc/passwdNF: Number of fieldsAwk-f: ' {print NF} '/etc/fstab, reference built-in variable without $
From: http://blog.chinaunix.net/space.php? Uid = 22133229 do = blog id = 1790099
Awk usage: awk 'pattern' {action }'
Variable name meaningNumber of ARGC command line VariablesARGV command line meta ArrayFILENAME current input file nameNumber of records in the current FNR FileThe input field delimiter of FS. The default Delimiter is a space.RS input record delimiterNumber of domains in the current NF reco
Awk array statistics, awk Array
Process the following file content, retrieve the domain name andDomain Name countSorting: (Baidu and sohu interview questions)
1 http://www.etiantian.org/index.html2 http://www.etiantian.org/1.html3 http://post.etiantian.org/index.html4 http://mp3.etiantian.org/index.html5 http://www.etiantian.org/3.html6 http://post.etiantian.org/2.html
Results:
mp3.etiantian.org 1post.etian
-font-family:"Times New Roman";mso-hansi-font-family:"Times New Roman";mso-ansi-language:#0400;mso-fareast-language:#0400;mso-bidi-language:#0400;}-->
1. Introduction
Retrieve various types of data from the original data file, remove the header, and output it to different files named according to the data type. This sorting and formatting task is a basic part of data analysis and processing. You can use awk, grep/SED/
is not started with your site's domain name string (in this case, www.example.com); All reference pages are displayed and the number of occurrences is counted. commands related to accessing IP addresses
How many different IP accesses are counted:
awk ' {print $} ' Access.log |sort|uniq|wc–l
Count the number of pages per IP accessed:
awk ' {++s[$1]} end {for (a in S) print A,s[a]} ' log_file
The number of
Tags: multiple delimiter reference command blank line type print greater than waterawk 一种编程语言Syntax format: awk "option" ' Pattern{action} ' "File" ... awk [option] ' condition {action} ' [file] ...Note: There must be at least one space between each element in the awk command and in subsequent options and filesOption Description:
parameter Options
awk Linux Three Musketeers boss filter, output content, a language. NR Representative Line numberThe first column, the second column, $ A, indicates that a row of NF indicates the last column after the specified delimiter-f Specifies the separator. = = is an assignment = = is the equal meaningWrite to: delimiter, first row in file passwd, 1th, 4 columns, and last column[[email protected] data]#
separator (the separator is saved in the variable FS, each segment is stored in a variable starting from $1 and can be referenced or formatted for output. Usage: awk [options] 'project' file... awk [option]... 'pattern' {action1; action2 ...} 'File... common options:-F fs: Specifies the input separator-v Var_name = VALUE: Custom variable-f scriptfile: load the awk
restriction of "condition type", the following "action" should be judged.
Finish all the action and condition types;
If there are subsequent "rows" of data, repeat the above steps until all the data has been read out.
After this step, you will know that awk is "the unit that behaves once", and "the smallest processing unit in a field." Okay, so how does awk know how many lines I have on this d
variables
There are two types of built-in variables in awk, one that can be changed as needed, mainly: FS: Field separator for input data, RS: Record delimiter for input data, OFS: field divider for output data, ORS: Record delimiter for output data, and system auto-change such as: NF: The number of fields currently recorded, NR: current record number, etc.
To illustrate:
field is a blank character (space, \ t), so each row of the input data represents a record, and the contents of each row are separated into multiple fields by whitespace. With fields and records, AWK has a very flexible way to work with filesSyntax 1 syntaxA typical awk syntax is as follows:awk ' BEGIN{STAT1} Pattern1{action1} pattern2{action2} ... Patternn{actionn} {def
Reprinted please indicate the source:Http://hi.baidu.com/leejun_2005/blog/item/702d4888928b15a60e2444b4.htmlRef: http://bbs.chinaunix.net/thread-108596-1-1.html1. Getline usage:Getline is a function used in the awk for input redirection. It can read the input from the standard input/a media transcoding queue/file, not just from the currently processed file, he gets the next line of the input and sets the value to the native variables such as NF,
ofEveryCharacterIs"EmptyWhiteKey " or "[ta key ", 1 indicates the logged-on user, $ $ means login User IP, and so on. $ last-n 5|awk ' {print $ "\ T" $ $} ' Lzyer 192.168.56.1reboot BootWtmp Sat$ last-n 5|awk ' {print $1,$3} ' lzyer 192.168.56.1reboot bootWtmp SatOnly the first row of data is processed$ last-n 5|awk '
Tags: awkHttp://www.cnblogs.com/ginvip/p/6352157.htmlThis is a blog I found, the content of awk is written very well, need to seeawk about! No.(1) Take odd lines[[email protected] shell]# SEQ 10 | awk ' I=!i '13579(2) Take even lines[[email protected] shell]# SEQ 10 | awk '! (i=!i) '246810(3) SEQ 10 not after 4 lines[[email protected] shell]# SEQ 10 | Head-n-4123
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.