I'm writing a bash script where I'm using mysql query in that script where the format which I'm getting is quite different.
Query:
root@debian:~# mysql -u root -ptoor super_market -h 0 -e "select * from items;"
+---------+------------+--------+-------------+-------+
| item_id | item_name | weight | brand | price |
+---------+------------+--------+-------------+-------+
| 1 | Milk | 2.00 | Nestle | 2.00 |
| 2 | Cheese | 2.50 | Amul | 6.00 |
| 3 | Chips | 25.00 | Lays | 3.00 |
| 4 | Coke | 5.00 | Coke Cola | 3.50 |
| 5 | Engage | 5.00 | Deo | 3.50 |
| 6 | Engage | 5.00 | Deo | 3.50 |
| 7 | Ear phones | 4.00 | Skull Candy | 32.30 |
+---------+------------+--------+-------------+-------+
formatted using column -t command the output format is getting aligned
root@debian:~# mysql -u root -ptoor super_market -h 0 -e "select * from items;" | column -t
item_id item_name weight brand price
1 Milk 2.00 Nestle 2.00
2 Cheese 2.50 Amul 6.00
3 Chips 25.00 Lays 3.00
4 Coke 5.00 Coke Cola 3.50
5 Engage 5.00 Deo 3.50
6 Engage 5.00 Deo 3.50
7 Ear phones 4.00 Skull Candy 32.30
Bash Script
the bash script which I tried using the above command
root@debian:~# cat test
#!/bin/bash
while read -r output;
do
echo $output | awk '{print $4}'
#do something
done< <(mysql -u root -ptoor super_market -h 0 -e "select * from items;" | sed 1d |column -t)
output
root@debian:~# ./test
Nestle
Amul
Lays
Coke
Deo
Deo
4.00
But the Expected Output:
Nestle
Amul
Lays
Coke Cola
Deo
Deo
Skull Candy
Yeah! you can say use select brand from items. This is for example in real time I'm using quite different command.
Any Hint or Help ?
Best Answer
output of mysql is tab delimited.
column
replaces tabs with spaces breaking your input.test run: