Aho Weinberger Kernighan

From 6bit.ch wiki
Revision as of 13:26, 21 August 2023 by Xbl (talk | contribs) (Created page with "= awk = is a full scripting language, as well as a complete text manipulation toolkit for the command line.</br> == Use == <code>who | awk '{print $1}'</code> print the first field of every line from the who command</br> <code>who | awk '{print $1,$4}'</code> print the first and fourth field of every line from the who command</br> <code>who | awk '{print $NF}'</code> print the last field (NF=Number of Fields) of every line from the who command</br> </br> <code>date | a...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

awk

is a full scripting language, as well as a complete text manipulation toolkit for the command line.

Use

who | awk '{print $1}' print the first field of every line from the who command
who | awk '{print $1,$4}' print the first and fourth field of every line from the who command
who | awk '{print $NF}' print the last field (NF=Number of Fields) of every line from the who command

date | awk 'OFS="-" {print$2,$3,$6}' print the date fields 2, 3 and 6 using the OFS (Output Field Seperator) '-' date | awk 'OFS="/" {print$2,$3,$6}' print the date fields 2, 3 and 6 using the OFS (Output Field Seperator) '/'
awk 'BEGIN {print "yoloooo"} {print $0} END {print "swaaag"}' test.sh 'BEGIN' prints "yoloooo" before any text processing, 'END' prints "swaaag" after text processing
awk -F: '$3 >= 1000 {print $1,$6}' /etc/passwd print field 1 and 6 from /etc/passwd where field 3 is greater than 1000

Useful Flags

-F: define field delimiter as ':'