[go: up one dir, main page]

0% found this document useful (0 votes)
547 views3 pages

Awk Cheatsheet

This document provides a cheat sheet for the Awk programming language, including: 1) Awk syntax and common options used to specify patterns, actions, and input files. 2) Built-in variables in Awk like FS, NR, and ARGV. 3) Operators, conditional statements, loops, and functions in Awk. 4) How to use associative arrays and sorting functions. 5) Formatting output with printf and special characters.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
547 views3 pages

Awk Cheatsheet

This document provides a cheat sheet for the Awk programming language, including: 1) Awk syntax and common options used to specify patterns, actions, and input files. 2) Built-in variables in Awk like FS, NR, and ARGV. 3) Operators, conditional statements, loops, and functions in Awk. 4) How to use associative arrays and sorting functions. 5) Formatting output with printf and special characters.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Awk Cheat Sheet www.thegeekstuff.

com
Awk
Awk syntax: awk [options] '[/pattern/] {action}' [input-file]
-F Specify field separator awk -F: '{print $1}' /etc/passwd
/pattern/ Execute actions only for matched pattern awk -F: '/root/ {print $1}' /etc/passwd
BEGI BEGI !lock is executed once at the start awk -F: 'BEG! { print "uid"} { print $1 }' /etc/passwd
E" E" !lock is executed once at the end awk -F: '{print $1} E!# { print "-done-"}' /etc/passwd
Awk built-in variables
FS
Input field separator# Specify the input-file field delimiter# "efault is space#
$FS $utput field separator# Fields printed out are separated usin% this &alue# "efault is space#
'S 'ecord separator# Specify the input file record delimiter# "efault is new line#
$'S $utput record separator# 'ecords printed out are separated usin% this &alue# "efault is new line#
F um!er of fields in the current record#
' um!er of the record# Indicates the total num!er of records processed at that point#
FI(EA)E *ontains current input-file that is %ettin% processed# Set to +-, while processin% from stdin/pipe#
F'
n %
File +um!er of the record,# For multiple files- F' is reset for e&ery file#
S.BSE/
n %
Su!script separator for array indexes# "efault is 012340
'S5A'5
n %
)atch function sets 'S5A'5 with the startin% location of str6 in str7#
'(EG58
n %
)atch function sets '(EG58 with len%th of the str6#
A'G*
n %
5otal num!er of ar%uments passed to awk script#
A'G9
n %
Array containin% all awk script ar%uments# Indexed from 2 throu%h A'G*
A'GI"
%
Index to A'G9 to retrie&e the current file name
$F)5
n %
Awk uses this to decide how to print &alues# "efault is 0:#;%0
E9I'$
%
Array containin% all en&ironment &aria!les and &alues# e#%# E9I'$<0/A580=
IG$'E*ASE
%
"efault is 2# >hen set to 6- it is case insensiti&e for strin% and re%-ex comparisons#
E''$
%
*ontains error messa%e of an I/$ operation# e#%# while usin% %etline function#
Awk operators
Strin% .se an empty space for strin% concatenation# e#%# str$ % str1 str&
Sed and Awk 101 Hacks Enhance your UNIX / !nu" !fe w!th Sed and Awk
Awk Cheat Sheet www.thegeekstuff.com
Assi%nment x ? x @ 7 is same as x += 7# Similarly use -?- A?- /?- and :?
*onditional B- B?- C- C?- ??- D?- EE Ffor andG- HH Ffor orG
'e%-ex ~ for match operator# ~= for +no match operator,
Awk conditional statements and loops
if if Fconditional-expressionG action
if else if Fconditional-expressionG action6 else action7
while while FconditionG action
do while do action while FconditionG
for for FinitialiIationJ conditionJ increment/decrementG action
!reak Kump out of the innermost loopin% that encloses it#
continue Skip o&er the rest of the loop !ody and start the cycle a%ain#
exit Stop executin% the script and exit
Awk associative arrays
Basics Assi%n element : arrayname<strin%=?&alue e#%# ite'[1()]%"*aser +rinter"
'efer element : &aria!le?arrayname<strin%= e#%# ',-ar%ite'[1()]
"elete element : delete arrayname<index= e#%# delete ite'[1()]
Browse elements : for F&ar in arraynameG actions e#%# for ./ in ite'0 print ite'[/]
asort asort function sorts the array &alues and stores them in indexes from 6 throu%h n# >here n is the total
num!er of elements in the array#
asorti Kust like sortin% array &alues- you can take all the array indexes- sort them- and store it in a new array
usin% asorti function#
Awk printf command
Format
specifier
s ? Strin% e ? Exponential floatin% point o ? $ctal
c ? Sin%le character f ? Fixed floatin% point x ? 8exadecimal
d ? "ecimal % ? .se either e or f : ? /ercenta%e sym!ol
Special
character
1n ? ewline 1t ? 5a! 1& ? 9ertical ta!
1! ? Backspace 1r ? *arria%e return 1f ? Form feed
um!er Fixed >idth# um!er immediately after :# e#%# 0:Ls0# By default it is ri%ht Mustified
- Fixed >idth Fleft MustifiedG# )inus sym!ol immediately after : and !efore num!er# e#%# 0:-Ls0
Sed and Awk 101 Hacks Enhance your UNIX / !nu" !fe w!th Sed and Awk
Awk Cheat Sheet www.thegeekstuff.com
2 (eadin% Ieros# Add a 2 FIeroG !efore the num!er# e#%# 0:2Ls0
Awk Functions
indexFstr6-str7G Gi&e location of str7 in str6
len%thFstr6G (en%th of the strin%
splitFstr-arr-sepG Splits the strin% FstrG usin% separator FsepG and stores output in the array FarrG
su!strFstr-loc-lenG Extracts num!er of characters FlenG from strin% FstrG- startin% at location FlocG
su!Fstr6-str7-&arG
n %
In the input strin% F&arG- str6 is replaced with str7- and output is stored !ack in &ar
%su!Fstr6-str7-&arG
n %
Same as su!- !ut %lo!al# It does multiple su!stitutions on the same input strin% F&arG#
matchFstr6-str7G
n %
'eturns positi&e num!er when str6 is present in str7#
toupperFstrG
%
*on&erts str to upper-case
tolowerFstrG
%
*on&erts str to lower-case
umeric initFnG- lo%FnG- sNrtFnG- sinFnG- cosFnG- atan7Fm-nG
'andom um!er randFG - random num!er !etween 2 and 6
srandFnG O %enerate random num!er from n
Additional Awk Commands
print /rint a record- or field- or &alue
HE
%
5wo way communication !etween awk command and external process
systemFcmdG Execute the .nix $S command FcmdG from awk
systimeFG
%
*urrent time in epoch time# *om!ine with strftime
%
# e#%# print strftimeF0:c0-systimeFGG
%etline 'ead the next line from the input-file# Sets P2- F- '- F'
%etline &ar 'ead next line from the input-file and store it in &aria!le F&arG# Sets '- F'
%etline C file
n %
'ead next line from another input-file# Sets P2- F
%etline &ar C file
n %
'ead next line from another input-file and store it in &aria!le F&arG#
cmd H %etline Execute .nix $S command FcmdG and store output in P2
n %
is A>Q and GA>Q $nly#
%
is GA>Q $nly#
Sed and Awk 101 Hacks Enhance your UNIX / !nu" !fe w!th Sed and Awk

You might also like