Friday, May 06, 2011

Removing header from mysql output in bash

So I had been using a conditional statement to remove the column name from the output of a mysql query in bash. I found today that the header can be suppressed using --silent and --skip-column-names parameters in the mysql. As shown in mysql command options
##MYSQL QUERY##
variable=$(
mysql -sN -u username --password=password << eof use databasename; SELECT id FROM tablename; eof)


It is possible to alter a variable using parameter substitution as follows :
variable=${variable#id}
variable=${variable#*$'\n'}
variable="${x//$'id\n'}"
The 1st just removes the string "id", the 2nd removes everything up to the first new line and the 3rd removes all instances of the string "id" +newline. As suggested at Linux Questions


When returning the results in a for loop, the -n parameter for echo, removes the new line that is otherwise there.

##for each##
for ARG in $variable
do
echo -n $ARG
done

No comments: