logo
🐧 Linux 命令

AWK

AWK Cheat Sheet - 快速参考指南,收录常用语法、命令与实践。

📂 分类 · Linux 命令🧭 Markdown 速查🏷️ 2 个标签
#awk#text
向下滚动查看内容
返回全部 Cheat Sheets

Getting Started

Have a try
SHELL
滚动查看更多
$ awk -F: '{print $1, $NF}' /etc/passwd

--
-F:Colon as a separator
{...}Awk program
printPrints the current record
$1First field
$NFLast field
/etc/passwdInput data file

{.left-text}

Awk program
CODE
滚动查看更多
BEGIN          {<initializations>}
   <pattern 1> {<program actions>}
   <pattern 2> {<program actions>}
   ...
END            {< final actions >}

Example

CODE
滚动查看更多
awk '
    BEGIN { print "\n>>>Start" }
    !/(login|shutdown)/ { print NR, $0 }
    END { print "<<<END\n" }
' /etc/passwd
Variables
BASH
滚动查看更多
          $1      $2/$(NF-1)    $3/$NF
           ▼          ▼           ▼
        ┌──────┬──────────────────┬───────┐
$0/NR ▶ │  ID  │  WEBSITE         │  URI  │
        ├──────┼──────────────────┼───────┤
$0/NR ▶ │  1   │  cheatsheets.zip │  awk  │
        ├──────┼──────────────────┼───────┤
$0/NR ▶ │  2   │  google.com      │  25   │
        └──────┴──────────────────┴───────┘

CODE
滚动查看更多
# First and last field
awk -F: '{print $1,$NF}' /etc/passwd

# With line number
awk -F: '{print NR, $0}' /etc/passwd

# Second last field
awk -F: '{print $(NF-1)}' /etc/passwd

# Custom string
awk -F: '{print $1 "=" $6}' /etc/passwd

See: Variables

Awk program examples
CODE
滚动查看更多
awk 'BEGIN {print "hello world"}'        # Prints "hello world"
awk -F: '{print $1}' /etc/passwd         # -F: Specify field separator

# /pattern/ Execute actions only for matched pattern
awk -F: '/root/ {print $1}' /etc/passwd

# BEGIN block is executed once at the start
awk -F: 'BEGIN { print "uid"} { print $1 }' /etc/passwd

# END block is executed once at the end
awk -F: '{print $1} END { print "-done-"}' /etc/passwd
Conditions
CODE
滚动查看更多
awk -F: '$3>30 {print $1}' /etc/passwd

See: Conditions

Generate 1000 spaces
CODE
滚动查看更多
awk 'BEGIN{
    while (a++ < 1000)
        s=s " ";
    print s
}'

See: Loops

Arrays
CODE
滚动查看更多
awk 'BEGIN {
   fruits["mango"] = "yellow";
   fruits["orange"] = "orange"
   for(fruit in fruits) {
     print "The color of " fruit " is " fruits[fruit]
   }
}'

See: Arrays

Functions
CODE
滚动查看更多
# => 5
awk 'BEGIN{print length("hello")}'
# => HELLO
awk 'BEGIN{print toupper("hello")}'
# => hel
awk 'BEGIN{print substr("hello", 1, 3)}'

See: Functions

Awk Variables

Build-in variables
--
$0Whole line
$1, $2...$NFFirst, second… last field
NRNumber of Records
NFNumber of Fields
OFSOutput Field Separator <br> (default " ")
FSinput Field Separator <br> (default " ")
ORSOutput Record Separator <br> (default "\n")
RSinput Record Separator <br> (default "\n")
FILENAMEName of the file
Expressions
--
$1 == "root"First field equals root
{print $(NF-1)}Second last field
NR!=1{print $0}From 2nd record
NR > 3From 4th record
NR == 1First record
END{print NR}Total records
BEGIN{print OFMT}Output format
{print NR, $0}Line number
{print NR " " $0}Line number (tab)
{$1 = NR; print}Replace 1st field with line number
$NF > 4Last field > 4
NR % 2 == 0Even records
NR==10, NR==20Records 10 to 20
BEGIN{print ARGC}Total arguments
ORS=NR%5?",":"\n"Concatenate records
Examples

Print sum and average

CODE
滚动查看更多
awk -F: '{sum += $3}
     END { print sum, sum/NR }
' /etc/passwd

Printing parameters

CODE
滚动查看更多
awk 'BEGIN {
    for (i = 1; i < ARGC; i++)
        print ARGV[i] }' a b c

Output field separator as a comma

CODE
滚动查看更多
awk 'BEGIN { FS=":";OFS=","}
    {print $1,$2,$3,$4}' /etc/passwd

Position of match

CODE
滚动查看更多
awk 'BEGIN {
    if (match("One Two Three", "Tw"))
        print RSTART }'

Length of match

CODE
滚动查看更多
awk 'BEGIN {
    if (match("One Two Three", "re"))
        print RLENGTH }'
Environment Variables
--
ARGCNumber or arguments
ARGVArray of arguments
FNRFile Number of Records
OFMTFormat for numbers <br> (default "%.6g")
RSTARTLocation in the string
RLENGTHLength of match
SUBSEPMulti-dimensional array separator <br> (default "\034")
ARGINDArgument Index
GNU awk only
--
ENVIRONEnvironment variables
IGNORECASEIgnore case
CONVFMTConversion format
ERRNOSystem errors
FIELDWIDTHSFixed width fields
Defining variable
CODE
滚动查看更多
awk -v var1="Hello" -v var2="Wold" '
    END {print var1, var2}
' </dev/null

Use shell variables

CODE
滚动查看更多
awk -v varName="$PWD" '
    END {print varName}' </dev/null

Awk Operators

Operators
--
{print $1}First field
$2 == "foo"Equals
$2 != "foo"Not equals
"foo" in arrayIn array

Regular expression

--
/regex/Line matches
!/regex/Line not matches
$1 ~ /regex/Field matches
$1 !~ /regex/Field not matches

More conditions

--
($2 <= 4 || $3 < 20)Or
($1 == 4 && $3 < 20)And
Operations

Arithmetic operations

  • +
  • -
  • *
  • /
  • %
  • ++
  • --

{.cols-3 .marker-none}

Shorthand assignments

  • +=
  • -=
  • *=
  • /=
  • %=

{.cols-3 .marker-none}

Comparison operators

  • ==
  • !=
  • <
  • >
  • <=
  • >=

{.cols-3 .marker-none}

Examples
CODE
滚动查看更多
awk 'BEGIN {
    if ("foo" ~ "^fo+$")
        print "Fooey!";
}'

Not match

CODE
滚动查看更多
awk 'BEGIN {
    if ("boo" !~ "^fo+$")
        print "Boo!";
}'

if in array

CODE
滚动查看更多
awk 'BEGIN {
    assoc["foo"] = "bar";
    assoc["bar"] = "baz";
    if ("foo" in assoc)
        print "Fooey!";
}'

Awk Functions

Common functions
FunctionDescription
index(s,t)Position in string s where string t occurs, 0 if not found
length(s)Length of string s (or $0 if no arg)
randRandom number between 0 and 1
substr(s,index,len)Return len-char substring of s that begins at index (counted from 1)
srandSet seed for rand and return previous seed
int(x)Truncate x to integer value
split(s,a,fs)Split string s into array a split by fs, returning length of a
match(s,r)Position in string s where regex r occurs, or 0 if not found
sub(r,t,s)Substitute t for first occurrence of regex r in string s (or $0 if s not given)
gsub(r,t,s)Substitute t for all occurrences of regex r in string s
system(cmd)Execute cmd and return exit status
tolower(s)String s to lowercase
toupper(s)String s to uppercase
getlineSet $0 to next input record from current input file.
User defined function
CODE
滚动查看更多
awk '
    # Returns minimum number
    function find_min(num1, num2){
       if (num1 < num2)
       return num1
       return num2
    }
    # Returns maximum number
    function find_max(num1, num2){
       if (num1 > num2)
       return num1
       return num2
    }
    # Main function
    function main(num1, num2){
       result = find_min(num1, num2)
       print "Minimum =", result

       result = find_max(num1, num2)
       print "Maximum =", result
    }
    # Script execution starts here
    BEGIN {
       main(10, 60)
    }
'

Awk Arrays

Array with index
CODE
滚动查看更多
awk 'BEGIN {
    arr[0] = "foo";
    arr[1] = "bar";
    print(arr[0]); # => foo
    delete arr[0];
    print(arr[0]); # => ""
}'
Array with key
CODE
滚动查看更多
awk 'BEGIN {
    assoc["foo"] = "bar";
    assoc["bar"] = "baz";
    print("baz" in assoc); # => 0
    print("foo" in assoc); # => 1
}'
Array with split
CODE
滚动查看更多
awk 'BEGIN {
    split("foo:bar:baz", arr, ":");
    for (key in arr)
        print arr[key];
}'
Array with asort
CODE
滚动查看更多
awk 'BEGIN {
    arr[0] = 3
    arr[1] = 2
    arr[2] = 4
    n = asort(arr)
    for (i = 1; i <= n ; i++)
        print(arr[i])
}'
Multi-dimensional
CODE
滚动查看更多
awk 'BEGIN {
    multidim[0,0] = "foo";
    multidim[0,1] = "bar";
    multidim[1,0] = "baz";
    multidim[1,1] = "boo";
}'
Multi-dimensional iteration
CODE
滚动查看更多
awk 'BEGIN {
    array[1,2]=3;
    array[2,3]=5;
    for (comb in array) {
        split(comb,sep,SUBSEP);
        print sep[1], sep[2],
        array[sep[1],sep[2]]
    }
}'

Awk Conditions

if-else statement
CODE
滚动查看更多
awk -v count=2 'BEGIN {
    if (count == 1)
        print "Yes";
    else
        print "Huh?";
}'

Ternary operator

CODE
滚动查看更多
awk -v count=2 'BEGIN {
    print (count==1) ? "Yes" : "Huh?";
}'
Exists
CODE
滚动查看更多
awk 'BEGIN {
    assoc["foo"] = "bar";
    assoc["bar"] = "baz";
    if ("foo" in assoc)
        print "Fooey!";
}'

Not exists

CODE
滚动查看更多
awk 'BEGIN {
    assoc["foo"] = "bar";
    assoc["bar"] = "baz";
    if ("Huh" in assoc == 0 )
        print "Huh!";
}'
switch
CODE
滚动查看更多
awk -F: '{
    switch (NR * 2 + 1) {
        case 3:
        case "11":
            print NR - 1
            break

        case /2[[:digit:]]+/:
            print NR

        default:
            print NR + 1

        case -1:
            print NR * -1
    }
}' /etc/passwd

Awk Loops

for...i
CODE
滚动查看更多
awk 'BEGIN {
    for (i = 0; i < 10; i++)
        print "i=" i;
}'

Powers of two between 1 and 100

CODE
滚动查看更多
awk 'BEGIN {
    for (i = 1; i <= 100; i *= 2)
        print i
}'
for...in
CODE
滚动查看更多
awk 'BEGIN {
    assoc["key1"] = "val1"
    assoc["key2"] = "val2"
    for (key in assoc)
        print assoc[key];
}'

Arguments

CODE
滚动查看更多
awk 'BEGIN {
    for (argnum in ARGV)
        print ARGV[argnum];
}' a b c
Examples

Reverse records

CODE
滚动查看更多
awk -F: '{ x[NR] = $0 }
    END {
        for (i = NR; i > 0; i--)
        print x[i]
    }
' /etc/passwd

Reverse fields

CODE
滚动查看更多
awk -F: '{
    for (i = NF; i > 0; i--)
        printf("%s ",$i);
    print ""
}' /etc/passwd

Sum by record

CODE
滚动查看更多
awk -F: '{
    s=0;
    for (i = 1; i <= NF; i++)
        s += $i;
    print s
}' /etc/passwd

Sum whole file

CODE
滚动查看更多
awk -F: '
    {for (i = 1; i <= NF; i++)
        s += $i;
    };
    END{print s}
' /etc/passwd
while
CODE
滚动查看更多
awk 'BEGIN {
    while (a < 10) {
        print "- " " concatenation: " a
        a++;
    }
}'

do...while

CODE
滚动查看更多
awk '{
    i = 1
    do {
        print $0
        i++
    } while (i <= 5)
}' /etc/passwd
Break
CODE
滚动查看更多
awk 'BEGIN {
    break_num = 5
    for (i = 0; i < 10; i++) {
        print i
        if (i == break_num)
            break
    }
}'
Continue
CODE
滚动查看更多
awk 'BEGIN {
    for (x = 0; x <= 10; x++) {
        if (x == 5 || x == 6)
            continue
        printf "%d ", x
    }
    print ""
}'

Awk Formatted Printing

Usage

Right align

CODE
滚动查看更多
awk 'BEGIN{printf "|%10s|\n", "hello"}'

|     hello|

Left align

CODE
滚动查看更多
awk 'BEGIN{printf "|%-10s|\n", "hello"}'

|hello     |
Common specifiers
CharacterDescription
cASCII character
dDecimal integer
e, E, fFloating-point format
oUnsigned octal value
sString
%Literal %
Space
CODE
滚动查看更多
awk -F: '{
    printf "%-10s %s\n", $1, $(NF-1)
}' /etc/passwd | head -n 3

Outputs

SHELL
滚动查看更多
root       /root
bin        /bin
daemon     /sbin
Header
CODE
滚动查看更多
awk -F: 'BEGIN {
    printf "%-10s %s\n", "User", "Home"
    printf "%-10s %s\n", "----","----"}
    { printf "%-10s %s\n", $1, $(NF-1) }
' /etc/passwd | head -n 5

Outputs

CODE
滚动查看更多
User       Home
----       ----
root       /root
bin        /bin
daemon     /sbin

Miscellaneous

Regex Metacharacters
  • \
  • ^
  • $
  • .
  • [
  • ]
  • |
  • (
  • )
  • *
  • +
  • ?

{.cols-3 .marker-none}

Escape Sequences
--
\bBackspace
\fForm feed
\nNewline (line feed)
\rCarriage return
\tHorizontal tab
\vVertical tab
Run script
SHELL
滚动查看更多
$ cat demo.awk
#!/usr/bin/awk -f
BEGIN { x = 23 }
      { x += 2 }
END   { print x }
$ awk -f demo.awk /etc/passwd
69

Also see

相关 Cheat Sheets

1v1免费职业咨询
logo

Follow Us

linkedinfacebooktwitterinstagramweiboyoutubebilibilitiktokxigua

We Accept

/image/layout/pay-paypal.png/image/layout/pay-visa.png/image/layout/pay-master-card.png/image/layout/pay-airwallex.png/image/layout/pay-alipay.png

地址

Level 10b, 144 Edward Street, Brisbane CBD(Headquarter)
Level 2, 171 La Trobe St, Melbourne VIC 3000
四川省成都市武侯区桂溪街道天府大道中段500号D5东方希望天祥广场B座45A13号
Business Hub, 155 Waymouth St, Adelaide SA 5000

Disclaimer

footer-disclaimerfooter-disclaimer

JR Academy acknowledges Traditional Owners of Country throughout Australia and recognises the continuing connection to lands, waters and communities. We pay our respect to Aboriginal and Torres Strait Islander cultures; and to Elders past and present. Aboriginal and Torres Strait Islander peoples should be aware that this website may contain images or names of people who have since passed away.

匠人学院网站上的所有内容,包括课程材料、徽标和匠人学院网站上提供的信息,均受澳大利亚政府知识产权法的保护。严禁未经授权使用、销售、分发、复制或修改。违规行为可能会导致法律诉讼。通过访问我们的网站,您同意尊重我们的知识产权。 JR Academy Pty Ltd 保留所有权利,包括专利、商标和版权。任何侵权行为都将受到法律追究。查看用户协议

© 2017-2025 JR Academy Pty Ltd. All rights reserved.

ABN 26621887572