Awk command Basics

Source: Internet
Author: User
Tags tmp file
Basic commands

Awk '{action}' input file Unconditionally execute action
Awk '/pattern/{action}' input file Execute action when pattern matches
Awk '(condition) {action}' input file Execute action when condition is set


Awk '/Li Si/{print $0}' TMP // output a line record containing the 'Li si' keyword 2 Li si70awk '/^ 2/{print $0}' tmp // output the record of a line starting with 2 2 Li Si 70

About fields:
The awk command splits each line into multiple fields. Each field is separated by a specified or default delimiter. The default Delimiter is tab and space. You can use $1, $2, to access the corresponding fields. The fields start from 1, and $0 indicates the entire row.

Built-in Variables

$0 Current row record
$1 ~ $ N The Nth field of the current record, which is separated by FS
FS The input field delimiter. The default Delimiter is space.
RS The input record delimiter. The default value is a line break.
OFS The delimiter of the output field is also a space by default.
ORS Output record separator. The default value is line break.
NF Number of fields in the current record
FNR Number of lines of processed files, limited to the current file
NR Number of lines of processed files, multiple files are accumulated
Argc Number of command line parameters
Argv Command Line Parameter Array
Filename Name of the current input file
Ignorecase Specifies whether to ignore the case sensitivity when matching.
Environ UNIX environment variables
Errno UNIX system error message
Convfmt Conversion format of numbers to strings
Ofmt Set the output format of a number
Rstart First String matched by the matched Function
Rlength String Length matched by the matched Function
Fieldwidths Blank separator string of the input field width
Argind Argv identifier of the currently processed file



















 
 




The tmp file contains: ID, name, scoreawk '{print filename, argc, argv [0], argv [1]} 'tmptmp 2 awk tmpawk-F ', ''{print NF, FS, FNR, FS, NR, RS} 'TMP tmpps:-F, specifying a comma as the field separator 3, 1, 13, 1, 2 awk '{print environ ["path"]} 'tmp/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin: /sbin:/bin:/usr/games:/usr/local/gamesawk '{convfmt = "% F"; printf "% s \ n ", 0.11} 'tmp0.1120.awk' {ofmt = "% 0.2f"; print 1/3} 'tmp0.33awk' {match ("tmp123tmp",/123/); print rstart, rlength} 'tmp4 3


Operator
=, + =,-=, * =,/=, % =, ^ = Assignment Statement
| Logic or
&& Logic and
~ ~! Match Regular Expressions and do not match Regular Expressions
<, <=,>, >= ,! =, = Relational operators
+ ,-,*,/, Addition, subtraction, multiplication, division
**, ^ Power
! Non-logical
++ ,-- Auto-increment and auto-Increment
$ Field Reference
Space String Connector
? : C-condition expression
In Whether a key value exists in the array
In? : Awk 'in in {A [1]; A [2]; print 1 in? "Yes": "no"} 'regular expression match: awk-F ","' {if ($3 ~ /70/) printf "% S score % d \ n", $2, $3} 'tmp Li siscore 70 power: awk-F ", "'{TMP = $3 ^ 2; If (TMP >= 6400) printf" % S score % d \ n ", $2, $3} 'tmp King Five score 80 space: awk' begin {print "1" 2 "} '12awk' begin {print" 1 "," 2 "} '1 2


Array


The awk array is a set of key-value pairs. The subscript of the array can be numbers and letters. The values and keywords are stored in an internal table that uses hash for key/value.

awk 'BEGIN{a[z]="sky123";print a[z];}'sky123


Redirection
awk 'BEGIN{print "1" >> "1.txt"}' | cat 1.txt1ls |awk 'FS==" "{print 1}'1
Process control statement 

When a break statement is used for a while or for statement, the exit program loop occurs. When a continue statement is used for a while or for statement, the program moves cyclically to the next iteration. Next can read the next input line and return to the top of the script. This avoids other operations on the current input row. Exit causes the main input to exit cyclically and transfers the control to the end (if the end module exists ). If the end module is not defined or the exit statement is applied to the end, the execution of the script is terminated.

Begin and end
Awk 'in in {action}/pattern/{action} end {action} 'input file
Awk 'in in {print "begin"}/Zhang San/{print $0} end {print "end"} 'tmpinin1, Zhang San, 60end

If:
if (condition1) {    action1} else if (condition2) {    action2} else {    action3}

awk 'BEGIN{if(FNR==1) print 1;else print 2;}'2


While
while(condition){    action3}
awk 'BEGIN{a=1;while(a<5){a++;if(a>3) break;}print a;}'4


For
For (variable in array) {action3} For (variable; condition; expression) {action3}
 

awk 'BEGIN{a[1]=100;a[2]=200;a[3]=300;for(i in a){if(i==2) continue;print a[i];}}'100300awk 'BEGIN{a[1]=100;a[2]=200;a[3]=300;for(i=1;i<=3;i++){if(i==3) exit;print a[i];}}'100200


Do
do{action3}while(condition)

awk 'BEGIN{a=1;do{a++;}while(a<5)print a;}'5


Awk command Basics

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.