I am trying to find out as to how I would print out the entire line from a CSV that contains a specific keyword. For example, I am attempting to only scrape the lines from the CSV file that contain "Back Bay". Here is the code
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream myfile("cabs.csv");
while (myfile.good())
{
string line;
string find = "Back Bay";
getline(myfile, line, ',');
if (strstr(line.c_str(),find.c_str()))
{
cout << line << endl;
}
}
}
Here is the snippet of the CSV file:
2.61,Lyft,1543428487883,South Station,Back Bay,27.5,1.0,2cccac4a-57dc-4dbe-b531-bb529335e898,lyft_lux,Lux Black
2.61,Lyft,1543428487883,South Station,Back Bay,7,1.0,38504f2f-300c-4e1b-bff4-4b44c3b4b842,lyft_line,Shared
2.61,Lyft,1543428487883,South Station,Back Bay,10.5,1.0,3ec303ac-a8b2-4fb8-add2-c83b61c627b5,lyft,Lyft
2.07,Lyft,1543428487883,North End,Beacon Hill,16.5,1.0,7b0c4e8d-21fc-4350-9972-f8dace1177ad,lyft_premier,Lux
2.07,Lyft,1543428487883,North End,Beacon Hill,10.5,1.0,9df49a6e-662d-4ac9-b003-34e3f146a209,lyft,Lyft
2.07,Lyft,1543428487883,North End,Beacon Hill,22.5,1.0,b7d55026-7809-4b7a-b275-27648f79cb8a,lyft_lux,Lux Black
So far my output only prints out the text "Back Bay"(however many times it occurs) but I would like it to also return the entire line of values. =>
from this:
Back Bay
Back Bay
Back Bay
to this:
2.61,Lyft,1543428487883,South Station,Back Bay,32.5,1.0,e1f1d1e3-f66a-4c12-94a6-62f59aff739b,lyft_luxsuv,Lux Black XL
2.61,Lyft,1543428487883,South Station,Back Bay,22.5,1.0,ed7e441e-8318-4c89-9499-8d4737bad7b5,lyft_premier,Lux
3.45,Uber,1543474677639,South Station,Back Bay,,1.0,09ede7b3-2e9b-43b4-8837-a58efb2e4d7a,8cf7e821-f0d3-49c6-8eba-e679c0ebcf6a,Taxi