Open ninonk-ncs opened 1 year ago
gsub(r,s)
substitutes s for r globally in current input line, returns the number of substitutions
gsub(r,s,t)
substitutes s for r in t globally, returns number of substitutions
index(s,t)
returns position of string t in s, 0 if not present
length(s)
returns length of s
match(s,r)
returns position in s where r occurs, 0 if not present
split(s,a)
splits s into array a on FS, returns number of fields
split(s,a,r)
splits s into array a on r, returns number of fields
sprintf(fmt, expr-list)
returns expr-list formatted according to format string specified by fmt
sub(r,s)
substitutes s for first r in current input line, returns number of substitutions
sub(r,s,t)
substitutes s for first r in t, returns number of substitutions
substr(s,p)
returns suffix s starting at position p
substr(s,p,n)
returns substring of s length n starting at position p
ls -lh | awk '{ printf "%-20s %s \n", $9, $5 }'
ls -lh
: list files with file size%-20s
: add 20 char padding~
Applications 96B
Desktop 704B
Developer 512B
Documents 2.9K
Downloads 10K
Library 2.8K
Movies 224B
Music 160B
Personal 1.2K
Pictures 192B
Postman 96B
Public 128B
Workspace 608B
big.log 87K
iCloud 192B
iCloud 192B
iCloud 160B
opt 96B
source 128B
Remove duplicate lines (stackoverflow)
awk '!x[$0]++'
Prefix with number for non blank lines (reference)
awk 'NF { $0=++a " :" $0 }; { print }'
awk '<filter> { <action> }' <input_file>
awk '/pattern/ { print $0 }' file
Set field separator(FS) as
-
:awk -F '-' ...
TAB
:awk -F '\t' ...
SPACE
:awk -F ' ' ...
Filter
awk '/FOO BAR/'
- filters lines that has 'FOO BAR'pattern { action }
Examples (from
man awk
)length($0) > 72
{ print $2, $1 }
/start/, /stop/