Basic use of awk

Source: Internet
Author: User

Awk command format:

Awk [Options] 'awk-script' input-file...

Options:

-F: Specifies the field separator domain separator.

-F: Specifies the awk-script file name.

Awk-Script: Specifies the awk command

Input-file: Specifies the awk input file


Structure of awk-script:

It consists of three parts: Begin, body, and end.

Begin format:

Begin {awk-commands}

Used to print the header

Initialize variable

The begin keyword must be capitalized.

Optional begin keywords

Body format:

/Pattern/{action}

Execute once for each line of input-File

End format:

End {awk-commands}

Print the end of a table

The end keyword must be capitalized.

The end keyword is optional.

Print command: print is equivalent to print $0, and $0 represents all records.

# cat employee.txt101,Johnny Doe,CEO102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales Manager# awk -F, ‘{print}‘ employee.txt101,Johnny Doe,CEO102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales Manager# awk -F, ‘{print $0}‘ employee.txt101,Johnny Doe,CEO102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales Manager


Print command: The position parameter $1 $2... $ n represents the 1st columns and 2nd columns of a record, respectively. column N

# awk -F, ‘{print $1,$2}‘ employee.txt 101 Johnny Doe102 Jason Smith103 Raj Reddy104 Anand Ram105 Jane Miller

 

Built-in variables of awk:

FS: input field separator

OFS: output field separator

RS: Input record separator input record Separator

ORS: Output record separator output record Separator

Nr: number of records Record Number

Filename: current file name input file name

FNR: file "number of records" indicates the number of records for the file.

NF: number of field fields


FS:

# awk ‘BEGIN { FS=","; print "UserID\tTitle\n------------" } { print $1,$3 } END { print "------------" }‘ employee.txt  UserID  Title------------101 CEO102 IT Manager103 Sysadmin104 Developer105 Sales Manager------------


OFS:

# awk ‘BEGIN { FS=","; OFS=":";print "UserID\tTitle\n------------" } { print $1,$3 } END { print "------------" }‘ employee.txt    UserID  Title------------101:CEO102:IT Manager103:Sysadmin104:Developer105:Sales Manager------------


RS:

# cat employee-one-line.txt                                           101,Johnny Doe:102,Jason Smith:103,Raj Reddy:104,Anand Ram:105,Jane Miller# awk ‘BEGIN{RS=":"}{print $1}‘ employee-one-line.txt        101,Johnny102,Jason103,Raj104,Anand105,Jane# awk ‘BEGIN{FS=",";RS=":"}{print $1}‘ employee-one-line.txt          101102103104105


ORS:

# cat employee-one-line.txt                                           101,Johnny Doe:102,Jason Smith:103,Raj Reddy:104,Anand Ram:105,Jane Miller# awk ‘BEGIN{FS=",";RS=":"}{print $1,$2}‘ employee-one-line.txt       101 Johnny Doe102 Jason Smith103 Raj Reddy104 Anand Ram105 Jane Miller# awk ‘BEGIN{FS=",";RS=":";ORS="\n----\n"}{print $1,$2}‘ employee-one-line.txt 101 Johnny Doe----102 Jason Smith----103 Raj Reddy----104 Anand Ram----105 Jane Miller----

 

Nr:

# awk ‘BEGIN {FS=",";print "-----------"}{print "Record",NR,"is",$1,$2,$3}‘ employee.txt        -----------Record 1 is 101 Johnny Doe CEORecord 2 is 102 Jason Smith IT ManagerRecord 3 is 103 Raj Reddy SysadminRecord 4 is 104 Anand Ram DeveloperRecord 5 is 105 Jane Miller Sales Manager


Filename:

# awk ‘BEGIN {FS=",";print "-----------"}{print FILENAME,"Record",NR,"is",$1,$2,$3}‘ employee.txt -----------employee.txt Record 1 is 101 Johnny Doe CEOemployee.txt Record 2 is 102 Jason Smith IT Manageremployee.txt Record 3 is 103 Raj Reddy Sysadminemployee.txt Record 4 is 104 Anand Ram Developeremployee.txt Record 5 is 105 Jane Miller Sales Manager

 

FNR:

# awk ‘BEGIN {FS=",";print "-----------"}{print FILENAME,"Record",NR,"is",$1,$2,$3}‘ employee.txt items.txt -----------employee.txt Record 1 is 101 Johnny Doe CEOemployee.txt Record 2 is 102 Jason Smith IT Manageremployee.txt Record 3 is 103 Raj Reddy Sysadminemployee.txt Record 4 is 104 Anand Ram Developeremployee.txt Record 5 is 105 Jane Miller Sales Manageritems.txt Record 6 is 101 HD Camcorder Videoitems.txt Record 7 is 102 Refrigerator Applicanceitems.txt Record 8 is 103 MP3 Player Audioitems.txt Record 9 is 104 Tennis Racket Sportsitems.txt Record 10 is 105 Laser Printer Office# awk ‘BEGIN {FS=",";print "-----------"}{print FILENAME,"Record",FNR,"is",$1,$2,$3}‘ employee.txt items.txt -----------employee.txt Record 1 is 101 Johnny Doe CEOemployee.txt Record 2 is 102 Jason Smith IT Manageremployee.txt Record 3 is 103 Raj Reddy Sysadminemployee.txt Record 4 is 104 Anand Ram Developeremployee.txt Record 5 is 105 Jane Miller Sales Manageritems.txt Record 1 is 101 HD Camcorder Videoitems.txt Record 2 is 102 Refrigerator Applicanceitems.txt Record 3 is 103 MP3 Player Audioitems.txt Record 4 is 104 Tennis Racket Sportsitems.txt Record 5 is 105 Laser Printer Office


NF: In the output result, the first column is the file name, the second column is the record number, and the third column is the number of fields for each record.

# awk -F, ‘{print FILENAME,NR,NF}‘ employee.txt employee.txt 1 3employee.txt 2 3employee.txt 3 3employee.txt 4 3employee.txt 5 3

 

Awk variables and operators:

Unary operator:

+: Returns the current number.

-: Negative number.

++: Auto-Increment

--: Auto-Subtraction

+:

# cat employee-sal.txt 101,Johnny Doe,CEO,10000102,Jason Smith,IT Manager,5000103,Raj Reddy,Sysadmin,4500104,Anand Ram,Developer,4500105,Jane Miller,Sales Manager,3000# awk ‘BEGIN{FS=","}{print +$4}‘ employee-sal.txt 100005000450045003000

 

-:

# awk ‘BEGIN{FS=","}{print -$4}‘ employee-sal.txt  -10000-5000-4500-4500-3000


++: Pre auto-increment, auto-increment first, and then print the variable

# awk ‘BEGIN{FS=","}{print ++$4}‘ employee-sal.txt  100015001450145013001

 

--: Pre auto-subtraction, First Auto-subtraction, and then printing the variable

# awk ‘BEGIN{FS=","}{print --$4}‘ employee-sal.txt   99994999449944992999

 

++: Post auto-increment: print the variables first, and then add

# awk ‘BEGIN{FS=","}{print $4++}‘ employee-sal.txt      100005000450045003000# awk ‘BEGIN{FS=","}{$4++;print $4++}‘ employee-sal.txt 100015001450145013001


--: Post auto-subtraction. Print the variables first and then auto-subtraction.

# awk ‘BEGIN{FS=","}{print $4--}‘ employee-sal.txt   100005000450045003000# awk ‘BEGIN{FS=","}{$4--;print $4--}‘ employee-sal.txt 99994999449944992999

 

Mathematical operations:

+: Add

-: Minus

*: Multiplication

/:

%: Modulo

# cat arit.awk BEGIN{ FS=","; OFS=","; item_discount=0;}{ item_discount=$4*20/100; print $1,$2,$3,$4-item_discount,$5-1} # cat items.txt 101,HD Camcorder,Video,210,10102,Refrigerator,Applicance,850,2103,MP3 Player,Audio,270,15104,Tennis Racket,Sports,190,20105,Laser Printer,Office,475,5# awk -f arit.awk items.txt  101,HD Camcorder,Video,168,9102,Refrigerator,Applicance,680,1103,MP3 Player,Audio,216,14104,Tennis Racket,Sports,152,19105,Laser Printer,Office,380,4


Character operation:

# cat string.awk BEGIN { FS=","; OFS=","; string1="Hello"; string2="World"; numberstring="100"; string3=string1" "string2; print "Concatenate string is: " string3; numberstring+=1; print "String to number: " numberstring; }# awk -f string.awk items.txt Concatenate string is: Hello WorldString to number: 101


Assignment operation:

=: Value assignment

+ =: A + = 1 is equivalent to a = a + 1

-=: A-= 1 equivalent to a = A-1

* =: A * = 2 is equivalent to a = A * 2

/=: A/= 2 is equivalent to a = A/2

% =: A % = 2 is equivalent to a = A % 2

# cat total.awk BEGIN { total1=total2=total3=total4=total5=10; total1+= 5; print total1; total2-= 5; print total2; total3*= 5; print total3; total4/= 5; print total4; total5%= 5; print total5;} # awk -f total.awk 155502


Comparison:

>:

>=:

<:

<=:

=:

! =:

&&:

|:

# cat items.txt 101,HD Camcorder,Video,210,10102,Refrigerator,Applicance,850,2103,MP3 Player,Audio,270,15104,Tennis Racket,Sports,190,20105,Laser Printer,Office,475,5# awk -F, ‘$5 <=5‘ items.txt 102,Refrigerator,Applicance,850,2105,Laser Printer,Office,475,5

 

# awk -F, ‘$4 < 200‘ items.txt    104,Tennis Racket,Sports,190,20

# awk -F, ‘$4 == 475 && $5 == 5‘ items.txt  105,Laser Printer,Office,475,5

 

# awk -F, ‘$4 == 475 || $5 == 15‘ items.txt   103,MP3 Player,Audio,270,15105,Laser Printer,Office,475,5

# awk -F, ‘$3 != "Video"‘ items.txt  102,Refrigerator,Applicance,850,2103,MP3 Player,Audio,270,15104,Tennis Racket,Sports,190,20105,Laser Printer,Office,475,5


Regular Expression operation:

~ : Match

!~ : Mismatch

# awk -F, ‘$3 ~ "ce"‘ items.txt 102,Refrigerator,Applicance,850,2105,Laser Printer,Office,475,5# awk -F, ‘$3 !~ "ce"‘ items.txt101,HD Camcorder,Video,210,10103,MP3 Player,Audio,270,15104,Tennis Racket,Sports,190,20


This article from the "Tiger, tiger, shengwei" blog, please be sure to keep this source http://tobeone.blog.51cto.com/817917/1553191

Basic use of awk

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.