2

I'm doing a small project using BASH for a "Phone Book". It stores the data in a mysql database and uses a temporary file to work with the Selects.

I'm using zenity visual interface, so I want to display the data stored in the database using the Zenity List Dialog. For doing that, I need to use the values of the columns individually, so I need to use the awk command to get those, but I'm having trouble in combining both zenity and awk commands.

Storing the values of the database, separated by \t in the temporary file.

echo "SELECT name,address,telephone,email FROM agenda" | mysql projAgenda -N -u root -p12345 >> tempAgenda.dat

Displaying the data using zenity --list

awk -F'\t' '{zenity --list --title="Listar registos" --text="" --column="Name" --column="Address" --column="Telephone" --column="E-mail" $1 $2 $3 $4}' tempAgenda.dat

But I'm always getting the same error:

awk: line 1: syntax error at or near =

Can someone help? I know I might be screwing up really big in trying to mix those two commands, but can't think of doing it another way.

EDIT: Output of the query (separated by tabs):

José Manel  Rua António Cão 219886868   zemaneli@live.com 
Cláudio Pinto   Praça Dom Rui da Camara 219886820   claudiopinto@live.com
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • 1
    Isn't zenity a shell command? Why are you trying to run it inside an awk script? – Mark Reed May 30 '15 at 14:14
  • 1
    Can you not run the zenity command and pipe it to awk? – rahul May 30 '15 at 14:29
  • edit your question to include 1-2 lines output from the `mysql` query. Good luck. – shellter May 30 '15 at 14:37
  • I edited the post shelter. The awk is working well. I've tried `awk -F'\t' '{ print "NAME: " $1 "\nADDRESS: " $2 "\nTELEPHONE:" $3 "\nE-MAIL: "$4 }' tempAgenda.dat` And it works. I'm really new to this, only started learning bash properly yesterday so I thought zenity would work like print, so I basically tried to replace it. I don't know how I can mix both commands, and I know it makes sense I can't put one inside another. What should I try? – Cláudio Pinto May 30 '15 at 15:08

1 Answers1

3

You need to replace the tab delimiter returned by mysql with a new line since zenity expects each column to be on a separate line when it reads from stdin. You can use tr for that:

mysql -N ... | tr '\t' '\n' | zenity --list --title="Listar registos" --text="" --column="Name" --column="Address" --column="Telephone" --column="E-mail" 

This would produce the following list:

enter image description here

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • That wouldn't do the job because the reason I'm displaying using the zenity list dialog is to show all of them in one window... but thanks anyway – Cláudio Pinto May 30 '15 at 15:39
  • I display all of them in one window. Have you tried it? – hek2mgl May 30 '15 at 15:40
  • Wait the comment I made just got outdated. Let me check. It looks good. – Cláudio Pinto May 30 '15 at 15:41
  • Yeah, I added the second solution later. I also couldn't believe that you want to display every contact in it's own window :) – hek2mgl May 30 '15 at 15:42
  • Yep! Problem solved! I didn't need to change the separator, since the values were separated by tabs I used `sed 's/\t/\n/g'` and it worked nicely Thanks a lot! – Cláudio Pinto May 30 '15 at 15:52
  • 1
    Yeah, I also figured that out :) .. I never knew that mysql will output tab separated data when output gets piped (or redirected to a file). Have also learned a lot in this post. Thanks too! ;) – hek2mgl May 30 '15 at 15:54
  • 1
    /offtopic I love the combination of Spanish and German here. Makes me wanna sing "we are the world" :P – Gerard van Helden May 30 '15 at 16:00