mysqllinuxubuntugrepmysql-error-1049

Grep on a joined table MySQL/Linux


I can't seem to get grep -v "Africa" on this table without throwing errors, can someone help me with syntax in Ubuntu?

There is an error somewhere, and one of my countries doesn't have a macroregion defined (i.e. Americas, Africa, Asia, Europe), and I can't find it. I need to grep this joined table to see which country only has sub regions.

select country, group_concat(region) as regions from regions group by country;

(footer of which looks like this)

| Ukraine                   | Eastern Europe,Europe
| United Arab Emirates      | Middle East,Persian Gulf,Western Asia
| United Kingdom            | Europe
| United States             | Americas,North America
| Uruguay                   | Americas,South America
| Uzbekistan                | Asia,Central Asia
| Vanuatu                   | Australasia,Melanesia,Oceania
| Venezuela                 | Amazon,Americas,Andes,South America
| Vietnam                   | Asia,East Asia,Indochina
| Virgin Islands            | Americas,Caribbean
| Wallis and Futuna Islands | Australasia,Oceania,Polynesia
| Yemen                     | Middle East,Persian Gulf,Western Asia
| Zambia                    | Africa,Central Africa,East Africa,Southern Africa
| Zimbabwe                  | Africa,Central Africa,East Africa,Southern Africa  

I use:

mysql -u root -pPASS "use reports"| mysql -u root -pPASS -e "select country, group_concat(region) as regions from regions group by country;"| grep -v 'Africa'

And I get the error:

ERROR 1049 (42000): Unknown database 'use reports' ERROR 1046 (3D000) at line 1: No database selected


Solution

  • mysql -u root -pPASS reports -e "select country, group_concat(region) as regions from regions group by country;" | grep -v 'Africa'
    

    first things first: you selected your database wrong. The database name is a positional parameter; you don't need to do an embedded sql statement.

    secondly, you can't use the mysql command in a pipeline and have the first command affect the second, since the database connection is closed prior to the second statement being executed; they're entirely different connections. Use a semicolon inside the statement if you need multiple queries. An alternative form of the aforementioned command that does so would look like this:

    mysql -u root -pPASS -e "use reports; select country, group_concat(region) as regions from regions group by country;" | grep -v 'Africa'