Awk call Syntax:
Awk [-F
-- Where:Ch: Field-separator character.Pgm: Awk command-line program.Pgm file: File containing an Awk program.Vars: Awk variable initializations.
Data file: Input data file.
Note:
1. The data file is optional. If not specified,
Awk string function (2), awk string Function
The substr function returns a substring starting from the specified position of the string. If the length of the substring is specified, the corresponding part of the string is returned. If the specified length exceeds the actual range of the string, the actual content is returned.FormatSubstr (string, starting position)
Substr (string, starting position, substri
Awk programming model and awk Programming
It is important to understand the basic model that awk provides to programmers. Learning awk is easier than learning other programming languages because awk provides programmers with well-defined and useful models.
The
Awk usage: awk 'pattern' {action} 'variable name meaning ARGC command line variable number ARGV command line variable element array FILENAME current input file name FNR current file record number FS input domain separator, default is a space RS input record delimiter NF number of fields in the current record NR number of records so far OFS output domain separator ORS output record separator 1
contents, which are not displayed in the order you expect. Arrays, like variables, are created automatically when they are used, and awk automatically determines whether they store numbers or strings. In general, an array in awk is used to collect information from records, which can be used to calculate sums, count words, and how many times the tracking template is matched.Show/ETC/PASSWD's account
).
ORS
The output record delimiter (the default value is a line break ).
RLENGTH
The length of the string matched by the match function.
RS
Record separator (a line break by default ).
RSTART
The first position of the string matched by the match function.
SUBSEP
Array subscript separator (default value: \ 034 ).
Awk calls each row ending with a line break a record.
Record separator: th
Simple examples of Text Analysis Tool awk and text awk
Create a file: vim hi
Take 2nd fields and 3rd fields:
Awk '{print $2, $3}' hi note {}, CommaWill be converted to a space in the output.
Character Description:
Display the entire line:
Field separator:-F
Built-in variable: NF: number of fields. $ NF indicates the last field.
NR: number of rows
/
The subscript of the awk associated array, and the subscript of the awk associated array
For more information about using variables as array indexes, see examples.
Example$ Cat employees
Tom Jones 4424 5/12/66 543354
Mary Adams 5346 11/4/63 28765
Sally Chang 1654 7/22/54 650000
Billy Black 1683 9/23/44 336500 $ awk '{name [x ++] = $2} END {for (I = 0; I 0 J
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
$
The awk Passes parameters to the script (2). The awk script Passes parameters.
An important limitation of command line parameters is that they are unavailable during the in process. That is, they are available only after the first line is entered. Why? This is a confusing part. Parameters passed from the command line are processed like file names. The value assignment operation is performed only when the va
Awk processes multiple rows of records and awk processes multiple rows
The records of input files used in all our examples are composed of a single row. In this section, we will demonstrate how to read a record, and each field in the record is composed of a single row.
We have learned an example of a file that processes the name and address. Let's assume that the same data is saved in a block format file. I
AWK and awk commands
VariableDescription
N the nth field of the current record. The fields are separated by FS.
0Complete input records.ARGCThe number of command line parameters.ARGINDLocation of the current file in the command line (starting from 0 ).ARGVArray containing command line parameters.CONVFMTNumber conversion format (default value: %. 6g)ENVIRONEnvironment Variable join array.ERRNODescript
Each awk statement consists of a pattern and corresponding actions. The execution of the pattern control rule in the awk-a rule is executed when its pattern matches the current input record.
Special begin and end modes:
They provide awk
ProgramStart and end actions,
The begin and end rules must have actions. They are executed only once in the program.
For ex
Multi-dimensional awk array and multi-dimensional awk
Although the awk command does not claim to support multi-dimensional arrays, it provides a method to define multi-dimensional arrays. Awk defines multi-dimensional arrays by concatenating multiple subscripts into strings. subscripts are separated by the value of the
UNIX awk User Manual (version 2)
Chi zhonglong
Author's words:
The UNIX awk User Manual was my work a few months ago. Looking back, this article is really rough, because I was too busy writing and uploading this work, without a good check on the quality of the article, I always feel that it is worthy of the love of netizens, so I am busy to rewrite it again, although it may not be a big change, however, the
Argc, argv
# Awk
ProgramThe available command line parameters are stored in the argv array. Argc is the number of command line parameters.
Argind
# The index of the currently processed file in argv. Each time gawk opens a new data file, it sets argind to the index in argv of the file name. That is to say, when gawk is processing files, 'filename = argv [argind] 'is always true.
Environ
# A joint array used to store environment variables. The dir
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
You may be familiar with Unix, but you may be unfamiliar with awk, which is not surprising. Indeed, compared with its excellent functions, awk is far from its proper popularity. What is awk? Unlike most other UNIX commands, we cannot know the functions of awk in terms of names: it is neither an independent English word
first 10 rows of the output file (analog head-n)
As mentioned earlier, the action is omitted here, which is the printout. The matching pattern is the variable nr needs less than 11,NR that is the current line number. The writing is simple, but there is a problem that when NR is greater than 10, awk actually makes a judgment on each line, and if the file is large, such as tens of thousands of lines, the wasted time is not negligible. So, the better
Invoking awk in a shell script is very natural and simple, and if you need to invoke the shell script/command, you need to use the system () function, which is written as System ("sh my.sh" $var) if you need to pass the variable to the called Shell. Note that there is a space before the second quotation mark.Awk calls the shell and passes the variable to the shell, and looking at the demo below, it's clear:
The code is as follows
Copy
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.