Clean and formatted output is essential in data processing, automation, and scripting. This guide will show you how to print MySQL output without headers or column separators using the MySQL command-line client options.
Why Print MySQL Output Without Headers or Column Separators?
When working with MySQL, there are times when you need raw data without any additional formatting. This is particularly useful for automation scripts, data exports, and integrating MySQL output into other tools or systems.
CLI Options to Print MySQL Output Without Headers or Column Separators
To achieve this, you’ll use a combination of MySQL command-line options. Here’s how:
-N
Option: This option tells MySQL not to print column names in the output.--silent
and--batch
Options: These options suppress additional formatting, making the output suitable for scripting.
Example Command
Here’s a basic example of a MySQL command to get output without headers or column separators:
mysql -u username -p -N -e "SELECT * FROM database.table" --silent --batch
In this command, we are passing the following options:
-u username -p
: Ask for the MySQL username and password.-N
: Do not display column names in the output.-e
: Execute the specified SQL query.--silent
and--batch
: Do not display additional formatting or column separators.
You can also use the -s
shorthand option instead of the --slient
flag, which does the same thing. The same MySQL command we saw above would look something like this with the -s
flag:
mysql -u username -p -sN -e "SELECT * FROM database.table"
Example outputs
Without passing the -s
and -N
flags, the MySQL CLI output would look something like this:
mysql> SELECT id, name FROM users;
+----+-------+
| id | name |
+----+-------+
| 1 | Rama |
| 2 | Guru |
| 3 | Kiran |
+----+-------+
3 rows in set (0.00 sec)
When you pass the -s
and -N
flags, the MySQL CLI will print MySQL output without headers or column separators.
mysql -u username -p -sN -e "SELECT id, name FROM users"
1 Rama
2 Guru
3 Kiran