Difference between revisions of "Aho Weinberger Kernighan"

From 6bit.ch wiki
Jump to navigation Jump to search
 
Line 8: Line 8:
<code>who | awk '{print $NF}'</code> print the last field (NF=Number of Fields) 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>
</br>
<code>date | awk 'OFS="-" {print$2,$3,$6}'</code> print the date fields 2, 3 and 6 using the OFS (Output Field Seperator) '-'</br>
<code>date | awk 'OFS="-" {print $2,$3,$6}'</code> print the date fields 2, 3 and 6 using the OFS (Output Field Seperator) '-'</br>
<code>date | awk 'OFS="/" {print$2,$3,$6}'</code> print the date fields 2, 3 and 6 using the OFS (Output Field Seperator) '/'</br>
<code>date | awk 'OFS="/" {print $2,$3,$6}'</code> print the date fields 2, 3 and 6 using the OFS (Output Field Seperator) '/'</br>
</br>
</br>
<code> awk 'BEGIN {print "yoloooo"} {print $0} END {print "swaaag"}' test.sh</code> 'BEGIN' prints "yoloooo" before any text processing, 'END' prints "swaaag" after text processing</br>
<code> awk 'BEGIN {print "yoloooo"} {print $0} END {print "swaaag"}' test.sh</code> 'BEGIN' prints "yoloooo" before any text processing, 'END' prints "swaaag" after text processing</br>

Latest revision as of 13:43, 21 August 2023

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 ':'