awk

This is a wiki page. Be bold and improve it!

If you have any questions about the content on this page, don't hesitate to open a new ticket and we'll do our best to assist you.

Table of Contents

Examples

with mysql

mysql -u myuser -p mytable -e "show table status where Engine='MyISAM';" | awk  'NR>1 {print "ALTER TABLE "$1" ENGINE = InnoDB;"}'

Where:
NR: The total number of input records seen so far.
So, NR>1 causes awk to skip the first line (where there are the unnecessary output headers).

The above script works if fields are separated by spaces, but if you use mysql to select a comment or text field which includes some spaces, the script will fail.
A solution is to get mysql to return tab-separated data, and use the tabs as field separator.
It means that on the mysql side, we need the --batch option.
On the awk side, we need to define the field separator NF.

This will output the third column:

mysql -u myuser -p mytable --batch -e "SELECT column1, column2, column3 FROM mytable; | awk 'NR>1 {print ""$3""}' FS="\t"

Processes

ps aux | awk '{print $2, $4, $11}' | sort -k2rn | head -n 10