In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to understand awk programming and related commands". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand awk programming and related commands".
First, what is awk?
Awk is a command under linux, its output of other commands, the processing of files are very powerful, in fact, he is more like a programming language, he can customize variables, conditional statements, loops, arrays, regularities, functions and so on. The way he reads the output, or the file, is to read it line by line, look it up according to the conditions you give, and operate in the found line, feeling that his design idea is really very simple, but combined with the actual situation, the concrete operation is not so simple. He has three situations, awk,gawk,nawk. What he usually calls awk is actually gawk.
Second, records, fields and separators in awk
When we read the output, or when we read the file, reading a line is a record. The record splitter is the carriage return character by default and is saved in RS,ORS.
We split out the words we want from the record, or phrases, etc., we call it the domain, domain separator, the default is spaces and TAB separators, and save the built-in changes
Quantity in ORS. For example:
Aaaa:bbbb:ccccccc
1111:2343:5t43343
There are two lines above, and these two lines are two records. The carriage return after each line is the record separator, and the colon is the domain separator. Things like aaaa,1111 are domains.
Awk-F:'{print $1} 'testfile
Third, built-in variables and operators of awk
1, variable
The variable describes the nth field of the current record of $n, separated by FS. $0 complete input record. The number of ARGC command line arguments. The location of the current file on the ARGIND command line (starting at 0). ARGV contains an array of command-line arguments. CONVFMT numeric conversion format (default is% .6g) ENVIRON loop variable associative array. Description of the last system error in ERRNO. List of FIELDWIDTHS field widths (separated by spacebar). FILENAME current file name. FNR is the same as NR, but relative to the current file. FS field delimiter (default is any space). If IGNORECASE is true, a case-ignoring match is performed. The number of fields in the current record of NF. The number of previous records by NR. The output format of OFMT numbers (default is% .6g). OFS outputs the field delimiter (the default is a space). ORS outputs the record delimiter (the default is a newline character). RLENGTH the length of the string matched by the match function. RS record delimiter (default is a newline character). RSTART the first position of the string matched by the match function. The SUBSEP array subscript delimiter (default is\ 034).
2, operator
Operator description = + =-= * / =% = ^ = * = assignment?: C conditional expression | | Logic or & & logic and ~ ~! Match regular expressions and mismatch regular expressions
< >=! = relative operator space concatenation +-add, subtract * / & multiply, divide and seek remainder + -! Unary addition, subtraction and logic are not ^ * exponentiated + +-increase or decrease, referencing members of the in array as prefix or suffix $fields
Fourth, the regularity of awk
Match description\ Y matches an empty string at the beginning or end of a word\ B matches an empty string within a word\ matches an empty string at the end of a word, anchoring the end\ W matches a non-alphanumeric word\ w matches an alphanumeric word\ 'matches an empty string at the end of the string\' matches an empty string at the beginning of the string
Fifth, the function of awk
1, string function
The function name describes the regular expression that sub matches the largest and leftmost substring in the record and replaces those strings with replacement strings. If no target string is specified, the entire record is used by default. Replacement occurs only in the first match when gsub matches throughout the document index returns the position where the substring is matched for the first time, the offset starts from position 1, substr returns the substring from position 1, if the specified length exceeds the actual length, the whole string split can split the string into an array according to the given delimiter. If no delimiter is provided, the number of characters that length returns the record by the current FS value match returns the index of the regular expression position in the string, or 0 if the specified regular expression is not found. The match function sets the built-in variable RSTART to the beginning of the substring of the string, and RLENGTH to the number of characters to the end of the substring. Substr can facilitate these variables to intercept strings toupper and tolower can be used for conversion between string sizes, this function is only valid in gawk
2, mathematical function
The function name returns the cotangent cos (x) cosine function exp (x) to the power of int (x) in the range of atan2 (x) y). The function name returns the integer log (x) Natural logarithm rand (x) Random number sin (x) sinusoidal sqrt (x) Square Root srand (x) x is the seed int (x) of rand () function, the process does not round rand () to produce a random number greater than or equal to 0 but less than 1.
Sixth, examples
When learning awk, I made an example of learning something. Don't just look at it. You can't remember anything just by looking at it. If you just look at it, maybe you know what's going on, but the real operation is that there is a problem either here or there. So be sure to do it yourself.
1. Test file test
The code is as follows:
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin:/bin:/bin/false
Daemon:x:2:2:daemon:/sbin:/bin/false
Mail:x:8:12:mail:/var/spool/mail:/bin/false
Ftp:x:14:11:ftp:/home/ftp:/bin/false
& nobody:$:99:99:nobody:/:/bin/false
Zhangy:x:1000:100:,:/home/zhangy:/bin/bash
Http:x:33:33::/srv/http:/bin/false
Dbus:x:81:81:System message bus:/:/bin/false
Hal:x:82:82:HAL daemon:/:/bin/false
Mysql:x:89:89::/var/lib/mysql:/bin/false
Aaa:x:1001:1001::/home/aaa:/bin/bash
Ba:x:1002:1002::/home/zhangy:/bin/bash
Test:x:1003:1003::/home/test:/bin/bash
@ zhangying:*:1004:1004::/home/test:/bin/bash
Policykit:x:102:1005:Po
Example 1:
The code is as follows:
Cat test | awk-F:'{\
If ($1 = "root") {\
Print $1;\
} else if ($1 = = "bin") {\
Print $2;\
} else {\
Print $3;\
}\
}'
Example 2:
The code is as follows:
Awk'{\
For (iTuno two) {
Return one
} else {
Return two
}
}
'
Example 6:
The code is as follows:
# awk 'BEGIN {print "what is your name"; getline name < "/ dev/tty"} $1 ~ name {print
# "found name on line" NR} END {print "see you" name} 'test
# awk'{sub (/ daemon/, "tankzhang"); print} 'test
# awk'{sub (/ zhangy/, "tankzhang"); $1}; print} 'test
# awk'{gsub (/ zhangy/, "tankzhang"); $1}; print} 'test
# awk-F:'{print index ("zhangy", $1)} 'test
# awk-F:'{print substr ($1m 1pm 2)} 'test
Awk-F:'{mat=match ($1 a-zA-Z / ^ [a-zA-Z] + $/); print mat,RSTART,RLENGTH} 'test
Example 7:
The code is as follows:
Cat test | awk-F:'\
NF! = 7 {\
Printf ("line% d fields:%s does not have 7 fields:%s\ n", NR,$0)}\
$1! ~ / ^ [A-Za-z0-9] / {printf ("line% d alpha and numeric user id:%s:% s\ n", NR,$1,$0)}\
$2 = "*" {printf ("lind% djin no password:%s\ n", NR,$0)}'
Thank you for your reading, the above is the content of "how to understand awk programming and related commands". After the study of this article, I believe you have a deeper understanding of how to understand awk programming and related commands, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.