INFO: The new Git default branch name is "main". Details here: https://about.gitlab.com/blog/2021/03/10/new-git-default-branch-name/

Commit 7e929e61 authored by Matteo Quintiliani's avatar Matteo Quintiliani
Browse files

Added other comments to readme.md

parent f9ec4196
......@@ -23,11 +23,22 @@ Examples of possible conversion specifiers:
The conversion specifiers `%s` within the *printf-like* strings have to be as many as the fields of the query result, except when using the option `-m` (see later).
`mysql_printf` is also able to process the escape sequence, such as `\n`, `\t`, etc.
Let's see some examples.
### Very simple example
Formatting query result to CSV (comma separated values). Use option `-e` followed by the separator character value.
mysql_printf -u myuser -p mypass -d mydbname \
-S "SELECT ... FROM ..." \
-e ','
### Simple example
Suppose we have the following MySQL table `People`:
Suppose you have the following MySQL table `People`:
```sql
CREATE TABLE People (
......@@ -43,23 +54,21 @@ Suppose we have the following MySQL table `People`:
('Jean-Marie', 'Ogret', 41, 'Paris');
```
and we want to format the values like this:
and you want to format the output like this:
Name: Mary Line Age: 47 City: New York
Name: Roberto Bianchi Age: 35 City: Milano
Name: Jean-Marie Ogret Age: 41 City: Paris
we can do this running `mysql_printf` with the following query:
Then, you can run `mysql_printf` with the following query:
SELECT Concat(FirstName, ' ', LastName), Age, City FROM People
and the following *printf-like* string format:
"Name: %-20s Age: %2s City: %-25s"
"Name: %-20s Age: %2s City: %-25s\n"
Follow the complete command line:
The complete command line is :
mysql_printf -u myuser -p mypass -d mydbname \
-S "SELECT Concat(FirstName, ' ', LastName), Age, City FROM People" \
......@@ -67,32 +76,19 @@ Follow the complete command line:
### Intermediate example
The following examples should explain better the possible usages.
Example of output formats for 14 attributes is:
Suppose you want to generate lists of people based on the city they live, a sort of *group by* city but saving them in different files, one for each different city. You can build the filename by the first field of your query result and run `mysql_printf` with the option `-m`:
```
%-5s %2s %1s%s %2s %7s%1s%3s %7s%1s %6s %s%s %-s
```
mysql_printf -u myuser -p mypass -d mydbname \
-S "SELECT Concat(Replace(City, ' ', '_'), '.txt'), Concat(FirstName, ' ', LastName), Age FROM People" \
-F "Name: %-20s Age: %2s\n" \
-m
Note: the number of conversion specifiers `%s` is one less than the number of fields of the query. The first field result is used for building the filename.
Example of command line:
mysql_printf -h host -d dbname -u user -p pass \
-S "SELECT name, age, sex FROM people;" \
-F "%-5s %2s %1s"
### Advanced example
mysql_printf -h host -d dbname -u user -p pass \
-S "SELECT name, age, sex FROM people;" \
-e ','
**TODO**: Writing output from in several files from multiple queries. Options `-m` and `-a`.
mysql_printf -h host -d dbname -u user -p pass \
-S "SELECT CONCAT(age, '.txt'), name, sex, 'nice' FROM nice_people;" \
-m
mysql_printf -h host -d dbname -u user -p pass \
-S "SELECT CONCAT(age, '.txt'), name, sex, 'nasty' FROM nasty_people;" \
-a -m
The two last commands create several files, one for each age,
containing all people with the same age.
## Dependencies and source compilation
......@@ -130,7 +126,8 @@ Thank you for considering contributing to this development.
## Author
Matteo Quintiliani - Istituto Nazionale di Geofisica e Vulcanologia - Italy
Mail bug reports and suggestions to <matteo.quintiliani@ingv.it>
Mail bug reports and suggestions to *matteo.quintiliani [at] ingv.it*
## License
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment