Output result with break line

Hello,

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:

check.log

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.sh

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

nCheck=$(awk '$2 != "OK" {print $1}' repairDBsF.log)
echo 'c '$nCheck
// this will return a result in one like:

// table2 table5 table7 table8

Is there anyway to have a result like:

Number of damaged tables are 4

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

The result should be line by line

Thanks in advance

grep -v "OK"

is another option to exclude lines with string "OK".

In your awk command, try printing entire line instead of just first field using just

print

or

print $0
1 Like

Try:

awk '$2!="OK"{t++; $2="(" $2; $NF=$NF ")"; s=s $0 "\n" } END{printf "The number of damaged tables is %s\n\n%s",t,s}' file

You can put it in a variable:

nCheck=$(awk '$2!="OK"{t++; $2="(" $2; $NF=$NF ")"; s=s $0 "\n" } END{printf "The number of damaged tables is %s\n\n%s",t,s}' file)

But be sure to put double quotes around the variable expansion when you print it, otherwise everything ends up in one line...

echo "$nCheck"
1 Like

My SQL is seriously archaic, but can't you do ALL of your query within SQL? Like

select ... where status != "OK";

This would give you the desired result incl. the count of defective tables.

Yes, correct, I am only concerned by the page display and the number of errors:

Number of damaged tables are 4  table2 (Some error here)  table5 (Another error)  table7 (Broken)  table8 (A very long error which take a 2 lines of multiple errors)

---------- Post updated at 01:14 AM ---------- Previous update was at 01:08 AM ----------

@Scrutinizer: could explain the code for me please?

@RudiC: I am looking for a bash code, if you could explain more please

Sure:

awk '
  $2!="OK" {             # if the second field is not equal to OK
    t++                  # increase total
    $2="(" $2            # prepend opening parenthesis to the second field
                         # and by assigning a value to one of the fields:
                         # recalculate $0 (which automatically removes excess spacing)
    $NF=$NF ")"          # append closing parenthesis to last field
    s=s $0 "\n"          # create one string "s" that contains all the information separated
                         # by newlines
  } 
  END {                  # After all lines are processes, print the total and the string
    printf "The number of damaged tables is %s\n\n%s",t,s
  }
' file
1 Like