ninokierulf / work-notes

My collection of learning and solutions to encountered issues
0 stars 0 forks source link

awk #11

Open ninonk-ncs opened 1 year ago

ninonk-ncs commented 1 year ago

awk '<filter> { <action> }' <input_file> awk '/pattern/ { print $0 }' file

Set field separator(FS) as

Filter

awk '/FOO BAR/' - filters lines that has 'FOO BAR'

pattern { action }

Examples (from man awk)

ninonk-ncs commented 1 year ago

Built in functions

reference

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

ninonk-ncs commented 1 year ago

ls -lh | awk '{ printf "%-20s %s \n", $9, $5 }'

Output in ~

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 
ninonk-ncs commented 1 year ago

Remove duplicate lines (stackoverflow) awk '!x[$0]++'

ninokierulf commented 1 year ago

Prefix with number for non blank lines (reference) awk 'NF { $0=++a " :" $0 }; { print }'