Awk is a powerful text analysis tool that reads the file line-by-row, using spaces as the default delimiter to slice each row, and then perform various analytical processing of the cut.
awk command form:
awk [-F |-f |-v] ' begin{}//{command1;command2; COMMANDN} end{} ' file
[-F |-f |-V]: Large parameter,-F specify delimiter,-F call script,-v define Variable
': Reference code block
BEGIN: Initializes the code block, initializing the code before processing on each line, (primarily referencing global variables in the script, setting the FS delimiter)
: Match style, can be a string or regular expression
{}: Command block of code containing one or more commands
;: Multiple commands are delimited with semicolons
End: The end code block, the code block executed after each row is processed, primarily for final calculation or output end summary information.
The arithmetic operations that AWK supports:
+ - * / ^ % ++ -- += -= *= /= %=
awk built-in variables:
The whole thing is going forward
$n nth field per row, N cannot be 0
NF Field number Variable
NR record number per row, multi-file processing is record increment
FNR record number per row, multi-file processing is not incremented, each file starts from 1
Defining delimiters when FS begin
The record delimiter entered by the RS, which defaults to line breaks
OFS output field delimiter, default is space
ORS output record delimiter, default is line break
Sample file Emp_names
$ cat Emp_names 46013 DURHAM jeff mobile al46015 Steen bill MOBILE al46017 FELDMAN evan mobile AL46018 SWIM steve unknown al46019 Bogue robert phoenix az46021 JUNE micah phoenix az46022 KANE sheryl unknown ar46024 WOOD WILLIAM muncie in46026 Fergus sarah muncie in46027 BUCK sarah muncie in46029 TUTTLE bob muncie in
$ Awk ' {print $} ' emp_names #打印文件emp_names中的所有字段46013 DURHAM JEFF MOBILE AL46015 Steen BILL MOBILE AL46017 FELDMAN EVAN MOBILE AL46018 SWIM STEVE UNKNOWN AL46019 Bogue ROBERT Phoenix AZ46021 JUNE Micah Phoenix AZ46022 KANE SHERYL UNKNOWN AR46024 WOOD WILLIAM MUNCIE IN46026 Fergus Sarah MUNCIE IN46027 BUCK Sarah MUNCIE IN46029 TUTTLE BOB MUNCIE in$ awk ' {print $1,$2,$3,$4,$5} ' emp_names #打印文件emp_names中多个字段46013 DURHAM JEFF Mobile AL46015 Steen BILL Mobile AL46017 FELDMAN EVAN Mobile AL46018 SWIM STEVE UNKNOWN AL46019 bogue ROBERT PHOENIX AZ460 JUNE Micah PHOENIX AZ46022 KANE SHERYL UNKNOWN AR46024 WOOD WILLIAM MUNCIE IN46026 Fergus SARAH MUNCIE IN46027 BUCK SAR AH MUNCIE IN46029 TUTTLE BOB MUNCIE in
$ Awk '/al|az/{print $3,$2,$5} ' emp_names multiple fields for #打印文件emp_names中匹配AL, az style Jeff DURHAM Albill Steen Alevan FELDMAN ALS TEVE SWIM alrobert bogue azmicah JUNE az$ awk ' $5~/ar/' emp_names #在第5个字段中匹配样式, no print fields are specified, all fields are printed 46022   &N Bsp KANE sheryl unknown ar$ awk ' $5!~/ar/' Emp_names #在第5个字段中匹配样式, Print out all rows that do not match (no print fields are specified, all fields are printed) 46013 DURHAM jeff mobile al46015 Steen bill MOBILE al46017 FELDMAN evan mobile al46018 SWIM steve unknown al46019 Bogue robert phoenix AZ46021 JUNE micah Phoenix &nbsP az46024 WOOD william muncie IN46026 Fergus sarah muncie in46027 BUCK sarah muncie in46029 TUTTLE bob muncie in$ awk ' $5~/ar/{print $; print $ 2; Print $ ' emp_names #将一行中多个字段分行打印46022KANESHERYL
$ Awk ' {print $ | "Sort >/tmp/filez"} ' emp_names #将文件emp_names中字段排序, redirect to/tmp/filez file in $ Cat/tmp/filez 46011 TUTTLE bob muncie in46012 Steen bill mobile AL46013 DURHAM jeff mobile al46017 FELDMAN evan mobile al46018 BOGUE ROBERT phoenix az46019 SWIM steve unknown al46021 JUNE micah phoenix az46022 KANE sheryl UNKNOWN ar46027 BUCK SARAH   &NBsp; muncie in46030 FERGUS SARAH muncie in46045 WOOD william MUNCIE in
$ awk-f ":" ' {print $} '/etc/passwd #/etc/passwd is the system comes with the file each line is used: delimited characters, so you need to specify the delimiter can also use awk ' {fs= ': '} {print $} '/etc /PASSWD So the result is the same rootdaemonbinsyssyncgamesmanlpmailnews ...
$ cat Awk.list begin{fs= ":"}{print $1}$ awk-f awk.list/etc/passwd #使用-F invoke awk script ROOTDAEMONBINSYSSYNCGAMESMANLP
$ awk ' {fs= ': '} {ofs= '-"}{print $1,$3} '/etc/passwd #指定输出字段分隔符root: x:0:0:root:/root:/bin/ bash-daemon-1bin-2sys-3sync-4games-5man-6lp-7$ awk-f ":" ' {print ' user_name: "$," \nnumber: "$ $} '/etc/passwd # Output user_name:root Number:0user_name:daemon number:1user_name:bin number:2user_name:sys Number:3user_nam in the format and content you want E:sync Number:4user_name:games Number:5
Sample file contents $ cat Inventory hammers57.99drills229.99punches73.59drifts24.09bits551.19saws12314.99nails800.19screws80.29brads100.24
$ Awk ' BEGIN {print "Item\tquantity\tprice\ttotal"} {x=x+ ($2*$3)} {print "\ T" $ "\t\t" $ "T" $2*$3} END {print "Total Val UE of Inventory: "x} ' inventoryitemquantitypricetotal # Begin keyword hammers57.9939.95drills229.9959.98punches73.5925.13drifts24.098.18bits551.1965.45saws12314.991843.77nails800.1915 2screws80.2923.2brads100.2424total value of inventory:2241.66 #END关键字 x Last value
AWK itself is a programming language, with support for IF statements, looping statements, and programming in C-like languages.
awk of Linux commands