Whether awk is a command or a programming language

Source: Internet
Author: User
Tags natural logarithm square root

awk is a command under Linux, his output of the other commands, the processing of the file is very powerful, in fact, he is more like a programming language, he can customize variables, conditional statements, there are loops, there are arrays, there are regular, there are functions and so on. He reads the output, or the way the file is a line, a line of reading, according to the conditions you give to find, and in the row to find out the operation, feel his design ideas, really simple, but combined with the actual situation, the specific operation is not so simple. He has three kinds of situations, Awk,gawk,nawk, and what he usually says about Awk is gawk.

II, records in awk, fields, separators

When we read the output, or read a file, reading a line is a record. The record separator is a carriage return by default and is saved in rs,ors.

We divide our records into words, phrases, etc., we call him domain, domain separator, default is Space and tab 銉, save in built-in change

Ors in volume. As an example:

Aaaa:bbbb:ccccccc

1111:2343:5t43343

There are two lines above, these two rows is two records, each row after the carriage return, is the record separator, inside the colon, is the domain separator, split out, aaaa,1111 this kind of thing is the domain.

Awk-f: ' {print '} ' testfile

Third, Awk's built-in variables and operators

1, variable

Variable description
$n The nth field of the current record, separated by FS between the fields.
$ The complete input record.
ARGC The number of command-line arguments.
Argind The location of the current file in the command line, starting at 0.
Argv An array that contains the command-line arguments.
Convfmt Number conversion format (default is%.6g)
ENVIRON An associative array of environment variables.
Errno Description of the last system error.
FieldWidths A list of field widths separated by a space key.
FILENAME The current file name.
FNR Same as NR, but relative to the current file.
Fs The field delimiter (the default is any space).
IGNORECASE If true, the matching of the case is ignored.
Nf The number of fields in the current record.
Nr The current number of records.
Ofmt The output format of the number (the default value is%.6g).
OFS The Output field delimiter (the default value is a space).
ORS The output record delimiter (the default value is a newline character).
Rlength The length of the string that is matched by the match function.
Rs Record delimiter (default is a line break).
Rstart The first position of a string that is matched by the match function.
Subsep Array subscript delimiter (the default value is \034).
2, operator operators describe
= += -= *= /= %= ^= **= Assign value
?: C-Conditional expression
|| Logical OR
&& Logic and
~ ~! Match regular expressions and mismatched regular expressions
< <= > >= = = = Relational operators
Space Connection
+ - Add, Subtract
*/& Multiply, divide and seek remainder
+ - ! Unary Plus, minus and logical non-
^ *** exponentiation
++ -- To increase or decrease, as a prefix or suffix.
$ Field reference
Inch Array members
Four, awk's regular match descriptor description
\y Match an empty string at the beginning or end of a word
\b Match an empty string within a word
\< Matches an empty string at the beginning of a word, anchoring begins
\> Matches an empty string at the end of a word, anchoring the end
\w Match a non-alphanumeric word
\w Match A word that consists of an alphanumeric number
\‘ Matches an empty string at the end of a string
\‘ Match an empty string at the beginning of the string
Five, awk function 1, string function function name Description
Sub A regular expression that matches the largest, leftmost substring in the record, replacing the strings with replacement strings. If you do not specify a target string, the entire record is used by default. Substitution only occurs at the time of the first match
Gsub Match throughout the document
Index Returns the position where the substring was first matched, offset starting at position 1
Substr Returns a substring starting at position 1, returning the entire string if the specified length exceeds the actual length
Split The string can be split into an array by the given delimiter. If the delimiter is not provided, it is split by the current FS value
Length Returns the number of characters in a record
Match Returns the index of the position of the expression 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 a substring of a string, rlength the number of characters to the end of the substring. SUBSTR can be beneficial for these variables to intercept the string
ToUpper and ToLower Can be used for conversions between string sizes, which are only valid in Gawk
2, Mathematical function function name return value
atan2 (x, y) Cotangent in the Y,x range
COS (x) cosine function
EXP (x) exponentiation
int (x) Take the whole
Log (x) Natural logarithm
RAND () Random number
Sin (x) Sinusoidal
sqrt (x) Square root
Srand (x) X is the seed of the rand () function
int (x) Rounding, process not rounded
RAND () Generates a random number that is greater than or equal to 0 and less than 1
VI, example

When I was studying awk, I did an example of learning something, not just seeing it, but not remembering things in light. Just look at the words, maybe you know what's going on, the real operation, not the problem here, is that there is a problem. So be sure to do it yourself.

1, test file testing

RootX:0:0: Root:/root:/bin/bashBinX:1:1: Bin:/bin:/bin/falseDaemonX:2:2:d Aemon:/sbin:/bin/falseMailX:8:12: Mail:/var/spool/mail:/bin/falseFtp:X:14:11: FTP:/home/ftp:/bin/false&Nobody$:99:99: Nobody:/:/bin/falseZhangy:X:1000:100:,,,:/home/zhangy:/bin/bashhttpX:33:33::/srv/http:/bin/falseDbusX:81:81: System Messagebus:/:/bin/falseHalX:82:82: HALdaemon:/:/bin/falseMysql:X:89:89::/var/lib/mysql:/bin/falseAaa:X:1001:1001::/HOME/AAA:/bin/bashBax:1002:1002 ::/home/zhangy:/bin/bash Test:x:1003:1003 ::/home/test:/bin/bash@ Zhangying:* :1004:1004::/home/test:/bin/bashpolicykit:x:102: 1005:P o            

Example 1:

Cat Test | Awk-f: ' {if ($ = = "Root") {print $;} else if (= = = "Bin") {print $;} else{print $;} '

Example 2:

awk ' {for (i=0;i<nf;i++) {if ($i ~/^root/) { print $i;} else if ($i ~/ zhangy/) {print $i; Continue }else if ($i ~/ mysql/) {print $i; Next }else if ($i ~/^ test/) {print $i; Break }}} ' test             

Example 3:

Tail Test | awk 'begin{while (getline d) {split (d,test); for (i in test) {print test[i]} }} '

Example 4:

Ls-al/home/zhangy/mytest | awk 'begin{while (getline d) {split (d,test); print test[9];}} '

Example 5:

' {print ' max = ', max ($1,$2)}function max (one,two) {if (one > a) {return one;} else{return;} '

Example 6:

#awk ' Begin{print ' What's your name "; Getline name <"/dev/tty "}$1 ~name{print line" Nr}end{print "See You" nam e} ' Test#awk ' {sub (/daemon/, "Tankzhang");p rint} ' test#awk ' {{sub (/zhangy/, "Tankzhang"); \ n};p rint} ' test  #awk ' {{gsub (/zhangy/, "Tankzhang"),;p rint} ' test #awk-f: ' {print index (' zhangy ', ' $ ')} ' Test' {mat=match ($, /^[a-za-z]+$/);p rint mat,rstart,rlength} ' test     

Example 7:

Cat test |awk-f: ' NF! = 7{printf ("line%d,does not has 7 fields:%s\n", nr,$0)}$1!~/^[a-za-z0-9]/{printf ("line%d,non A Lpha and numeric user id:%s:%s\n ", nr,$1,$0)}$2 = =" * "{printf (" Lind%d,no password:%s\n ", nr,$0)} '

Reprint Please specify

Author: submarine Eagle

Address: http://blog.51yip.com/shell/985.html

    • This article is from: Linux Learning Network

Whether awk is a command or a programming language

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.