I'm trying to convert the following bash code into C++ using boost::iostreams:
#!/usr/bin/bash
(
gzip -cd file1.ext.gz
cat file2.ext
) | grep '^regex' # or sed 's/search/replace/'
I can open a file and decompress it:
std::ifstream s("file.ext.gz", std::ios_base::in | std::ios_base::binary);
boost::iostreams::filtering_istreambuf in;
in.push(boost::iostreams::gzip_decompressor());
in.push(s);
Then open an uncompressed file:
std::ifstream s2("file.ext", std::ios_base::in | std::ios_base::binary);
Now I'm a bit stuck, so here are my questions:
1) What's the boost::iostreams solution to concat the two streams?
2) How to output the result through a regex filter to emulate grep/sed?
As a result I'd like to have a an istream that i can copy to cout:
boost::iostream::copy(result, std::cout);
UPDATE complete solution using Hamigaki's concatenate:
/*
* convert the following bash script into C++
*
* #!/bin/bash
* (
* gzip -cd file1.ext.gz
* cat file2.ext
* ) | grep '^filter' | 'sed s/search/replace/g'
*
*/
#include <iostream>
#include <boost/bind.hpp>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/iostreams/filter/regex.hpp>
#include <boost/iostreams/filter/grep.hpp>
#include <boost/iostreams/copy.hpp>
// http://hamigaki.sourceforge.jp/hamigaki/iostreams/concatenate.hpp
#include "concatenate.hpp"
namespace io = boost::iostreams;
int main(int argc, char const* argv[])
{
io::file_source file1("file1.ext.gz");
io::file_source file2("file2.ext");
io::gzip_decompressor gzip;
io::regex_filter sed(boost::regex("search"), "replace");
io::grep_filter grep(boost::regex("^filter"));
io::filtering_istreambuf in1(gzip | file1);
io::filtering_istreambuf in2(file2);
io::filtering_istreambuf combined(sed | grep |
hamigaki::iostreams::concatenate(
boost::ref(in1),
boost::ref(in2)
)
);
io::copy(combined, std::cout);
return 0;
}
zgrep
(unless, of course, you need the file to be uncompressed)? Won't your script send the file togrep
twice? – Minimafile.ext.gz
andfile.ext
and took that to mean the second was the result of the first (I overlooked the-c
). It would have been clearer if you had saidfile1.ext.gz
andfile2.ext
. – Minima