Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am coding a script to check mysql databases using bash script, I would like to check if the status of a table is not 'OK', will return the table name and do some more actions:


table1                 OK
table2                 Some error here
table3                 OK
table4                 OK
table5                 Another error
table6                 OK
table7                 Broken
table8                 A very long error which take a 2 lines
of multiple errors

# check and repair tables
mysqlcheck -u $hostUser -p$hostPasswd --all-databases --check --auto-repair >> check.log

# grep tables status, should return OK
cat check.log | grep 'OK' | awk '{print $2}'| while read status; do

# if any table does not contain OK
if [ "$status" != "OK" ]
##### HERE WHERE I am stuck
#### It loops and return all the table

# grep to get the table name
cat check.log | grep 'OK' | awk '{print $1}'| while read table; do
echo $tblName' is BROKEN'
echo $tblName' is FINE'


// here I need to mail the tables that have problems in one single mail

Looking for an output like:

tableName: error
tableName: error

table2: Some error here
table5: Another error
and so on

These lines will be an output into a log file (result.txt)

Thanks for your support

share|improve this question
Do you have a specific reason to use awk? – Jacob Vlijm Mar 30 at 6:38
Well if there is a better method, why not – Abu Rayane Mar 31 at 2:15

You don't need to read file line-by-line using bash. Awk does that already. For instance, you can print broken entries using this code

awk '$2!="OK"{print $0}'  check.log

You can also just print the table name and use that command to create an array, then act upon those:

array=( $(awk '$2!="OK"{printf "%s ",$1 }'  check.log) )

Note the space next to %s, that's important.

You mentioned also, that error can be multiline, just like for table8. We probably want to avoid messing with that. So we may add a check also for field 1 to contain a word table.

$> array=( $(awk '$1~/table/ && $2!="OK"{printf "%s ",$1 }'  check.log) )      
$> echo ${array[@]}                                                            
table2 table5 table7 table8
$> echo ${array[0]}                                                            
share|improve this answer
Oh yeah, awesome, how can I print the content of the array? – Abu Rayane Mar 30 at 4:17
Either echo or printf will do. See my edit – Serg Mar 30 at 5:38
Tables can have different names, I am looking to get a result like the above mentioned in my edited question – Abu Rayane Mar 30 at 6:34
@AbuRayane that's it ? You just want lines like tableX Error#25 ? – Serg Mar 30 at 6:41
In that case , awk '$2!="OK"{print $0}' check.log, my first code is what you want. – Serg Mar 30 at 6:42

Since awk's default behaviour is to print the full line when a True condition happens, you can say something as simple as this:

awk '$NF != "OK"' file

Which returns the following for your given input:

table2                 Some error here
table5                 Another error
table7                 Broken
table8                 A very long error which take a 2 lines
of multiple errors


  • NF stands for number of fields, so $NF stands for field number NF. That is, the last field on every line.
  • Saying $NF != "OK" we are checking if the last field on the line is "OK" or not. If it is not, the condition is true and the line gets printed automatically.
share|improve this answer
It is a good toot, I am looking to print the result per rows, as I edited in my question above, see the part "Looking for an output like:" – Abu Rayane Mar 31 at 2:00
@AbuRayane you should edit your question to show what exact output you want. Currently it is unclear. – fedorqui Mar 31 at 9:34
The solutions that you give and Serg as well gave me, are correct, it is only a matter of a display, I need to display the result line by line, i.e: table2: error then a new line table3: error another new line table5: error and so on – Abu Rayane Apr 1 at 3:25
@AbuRayane so instead of the full error you just want to display the string "error"? – fedorqui Apr 1 at 11:19
No, the full error, but line by line display as mentioned above – Abu Rayane Apr 2 at 2:54

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.