# => 5
awk 'BEGIN{print length("hello")}'
# => HELLO
awk 'BEGIN{print toupper("hello")}'
# => hel
awk 'BEGIN{print substr("hello", 1, 3)}'
See: Functions
awk 'BEGIN{
while (a++ < 1000)
s=s " ";
print s
}'
See: Loops
awk -F: '$3>30 {print $1}' /etc/passwd
See: Conditions
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
$1 $2/$(NF-1) $3/$NF
â¼ â¼ â¼
ââââââââ¬âââââââââââââââ¬ââââââââ
$0/NR ⶠâ ID â WEBSITE â URI â
ââââââââ¼âââââââââââââââ¼ââââââââ¤
$0/NR ⶠâ 1 â quickref.me â awk â
ââââââââ¼âââââââââââââââ¼ââââââââ¤
$0/NR ⶠâ 2 â google.com â 25 â
ââââââââ´âââââââââââââââ´ââââââââ
# 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
BEGIN {<initializations>}
<pattern 1> {<program actions>}
<pattern 2> {<program actions>}
...
END {< final actions >}
awk '
BEGIN { print "\n>>>Start" }
!/(login|shutdown)/ { print NR, $0 }
END { print "<<<END\n" }
' /etc/passwd
$ awk -F: '{print $1, $NF}' /etc/passwd
- | - | - |
---|---|---|
-F: |
Colon as a separator | |
{...} |
Awk program | |
print |
Prints the current record | |
$1 |
First field | |
$NF |
Last field | |
/etc/passwd |
Input data file |