Index: trunk/FACT++/src/rootifysql.cc
===================================================================
--- trunk/FACT++/src/rootifysql.cc	(revision 19117)
+++ trunk/FACT++/src/rootifysql.cc	(revision 19118)
@@ -35,4 +35,5 @@
         ("ignore",        vars<string>(),              "Ignore the given columns")
         ("display,d",     po_switch(),                 "Displays contents on the screen (most usefull in combination with mysql statements as SHOW or EXPLAIN)")
+        ("write",         var<string>(""),             "Write output to an ascii file")
         ("null,n",        po_switch(),                 "Redirect the output file to /dev/null (mainly for debugging purposes, e.g. performance studies)")
         ("no-fill",       po_switch(),                 "Do not fill events into the root file (mainly for debugging purposes, e.g. performance studies)")
@@ -93,5 +94,6 @@
         "$TEST or ${TEST} by '1, 2, 3'. This is useful for the SQL `IN` keyword. "
         "You can also read the values for an enviroment substitution from a file "
-        "(one element per line), e.g. --env.TEST=file.txt\n"
+        "(one element per line), e.g. --env.TEST=file.txt. Lines starting with a # "
+        "are skipped.\n"
         "\n"
         "Comments in the query-file can be placed according to the SQL standard inline "
@@ -373,4 +375,5 @@
     const bool     update      = conf.Get<bool>("update");
     const bool     display     = conf.Get<bool>("display");
+    const string   write       = conf.Get<string>("write");
     const bool     noout       = conf.Get<bool>("null");
     const bool     nofill      = conf.Get<bool>("no-fill");
@@ -420,28 +423,31 @@
 
     map<string, vector<string>> envs;
+
     for (const auto &env : conf.GetWildcardOptions("env.*"))
-        envs.emplace(env.substr(4), conf.Vec<string>(env));
+        envs[env.substr(4)] = conf.Vec<string>(env);
 
     for (const auto &env : conf.GetWildcardOptions("list.*"))
     {
-        const string fname = conf.Get<string>(env);
+        const string  fname = conf.Get<string>(env);
+        const string &ident = env.substr(5);
 
         ifstream fin(fname);
         if (!fin)
         {
-            cerr << "Could not open environment in '" << fname << "': " << strerror(errno) << endl;
+            cerr << "Could not open environment in '" << fname << "' for ${" << ident << "}: " << strerror(errno) << endl;
             return 3;
         }
 
-        const string &ident = env.substr(5);
-
-        string repl;
         for (string line; getline(fin, line); )
-            envs[ident].push_back(line);
+            if (Tools::Trim(line)[0]!='#')
+                envs[ident].push_back(line);
+
+        if (verbose>0)
+            cout << "Found " << envs[ident].size() << " list element(s) for ${" << ident << "}" << endl;
     }
 
     for (const auto &env : envs)
     {
-        regex rexpr("\\$\\{"+env.first+"\\}|\\$"+env.first+"\\b");
+        regex rexpr("\\$(\\{"+env.first+"\\}|"+env.first+"\\b)");
         query = regex_replace(query, rexpr, boost::join(env.second, ", "));
     }
@@ -460,8 +466,8 @@
     {
         FileStat_t stat;
-        const Int_t  exist = !gSystem->GetPathInfo(path, stat);
-        const Bool_t write = !gSystem->AccessPathName(path,  kWritePermission) && R_ISREG(stat.fMode);
-
-        if ((update && !exist) || (update && exist && !write) || (force && exist && !write))
+        const Int_t  exist  = !gSystem->GetPathInfo(path, stat);
+        const Bool_t _write = !gSystem->AccessPathName(path,  kWritePermission) && R_ISREG(stat.fMode);
+
+        if ((update && !exist) || (update && exist && !_write) || (force && exist && !_write))
         {
             cerr << "File '" << path << "' is not writable." << endl;
@@ -723,4 +729,8 @@
     }
 
+    ofstream fout(write);
+    if (!fout)
+        cout << "WARNING: Writing to '" << write << "' failed: " << strerror(errno) << endl;
+
     if (display)
     {
@@ -732,4 +742,12 @@
     }
 
+    if (!write.empty())
+    {
+        fout << "#";
+        for (size_t i=0; i<l.size(); i++)
+            fout << ' ' << l[i].c_str();
+        fout << endl;
+    }
+
     // ---------------------- Fill TTree with DB data --------------------------
     size_t count = 0;
@@ -744,5 +762,5 @@
         for (auto col=row.begin(); col!=row.end(); col++, idx++)
         {
-            if (display)
+            if (display || !write.empty())
             {
                 if (idx>0)
@@ -789,4 +807,6 @@
             if (display)
                 cout << sout.str() << endl;
+            if (!write.empty())
+                fout << sout.str() << '\n';
         }
 
