0

I would like to remove all lines marked by -- or [:space:]{1,}-- and line breaks and return one line file for example for the sample data below

Sample data

-- Query taken from:
-- some stack page http://www.blablabla.somethingelse.com
SELECT country.name as country, country.headofstate
from country
    -- Worst place to add comment ever
  -- Here is even uglier
inner join city on city.id = country.capital
where city.population > 100000
-- this comment makes no sense here and would break sql parser buyt hey
and country.headofstate like 'A%'
-- WOW!

the returned text should correspond to:

desired results

SELECT country.name as country, country.headofstate from country inner join city on city.id = country.capital where city.population > 100000 and country.headofstate like 'A%'

Error

When attempting to compile the code below using:

clang++ -Wall -std=c++11 line_skip.cpp -o lineskip && ./lineskip tst.sql

I get the error message:

ndefined symbols for architecture x86_64:
  "boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::do_assign(char const*, char const*, unsigned int)", referenced from:
      boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::assign(char const*, char const*, unsigned int) in line_skip-771400.o
ld: symbol(s) not found for architecture x86_64
clang-5.0: error: linker command failed with exit code 1 (use -v to see invocation)

Code

/*
 * Trivial example of a simple file skipping lines. The lines to be skipped 
 * should have ## as first non-blank characters everything else should be later
 * returned as one line text.
 *
 * To copy (test and run):
 * clang++ -Wall -std=c++11 line_skip.cpp -o lineskip && ./lineskip tst.sql
 */

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <boost/regex.hpp>
#include <boost/algorithm/string/replace.hpp>
using namespace std;

int main(int argc, char *argv[]) {
    cout << "Reading file: " << argv[1] << endl;

    // Read file to stringstream, as per: 
    // https://stackoverflow.com/a/132394/1655567
    std::ifstream file( argv[1] );

    if ( file )
    {
        std::stringstream buffer;

        buffer << file.rdbuf();

        file.close();

        // Declare string variable to loop trhough and delete carriage returns
        std::string readText;
        readText = buffer.str();

        // Regular expressions to pass to replace_all to remove all comments and line breaks
        boost::regex re_comment("^(\t{1,}|[:space:]{1,}|)--.*(\n|\r|$)");
        boost::regex re_lineend("(\n|\r)"); 

        // Remove line breaks and return clean string
        boost::replace_all(readText, re_comment, " ");
        boost::replace_all(readText, re_lineend, " ");

        // Show current text
        cout << "Text with no spaces and no comments:\n" << readText << endl;

        return 0;
    }

}
O'Neil
  • 3,790
  • 4
  • 16
  • 30
Konrad
  • 17,740
  • 16
  • 106
  • 167
  • Did you properly build the boost library ahead of time? There is a subset of the boost library that requires its libraries to be built ahead of time, and boost.regex may be part of that subset. – Xirema Jan 09 '18 at 17:46
  • The compiler is telling you it couldn't find a symbol, that looks like it comes from the Boost library. You should consider telling clang to link your Boost library. Try searching for _link boost regex_, and/or read your compiler docs for help with that. – Useless Jan 09 '18 at 17:50
  • @Xirema I’ve installed boost through homebrew. I may try to reinstall it and see if it works. – Konrad Jan 09 '18 at 18:18
  • 5
    I think you just need `-lboost_regex` in your link command. – G.M. Jan 09 '18 at 18:27
  • @G.M. Your solution works, I'll be happy to accept if you wish to make an answer. – Konrad Jan 10 '18 at 07:55

0 Answers0