I am trying extract some lines from plain text file. where it contains list of shell script executables with some generic keys for that particular sh file. the required data need to extract from that file should exclude .sh file name and MH_TEST keys.
for ex: if my file abc.lst contains
Cable_pull1.sh
MH_TEST PAR
DUAL_DOMAIN yes
CAMARO_STORAGE YES
Flagship.sh
MH_TEST NOR
10_Flags yes
Domain_Distibute.sh
MH_TEST NOR
fast_path YES
heavy_IO YES
the requested data needed to extract from above file abc.lst file is below, if file name passed is "Cable_pull1.sh"
DUAL_DOMAIN yes
CAMARO_STORAGE YES
if file name is passed "Flagship.sh", expected output is below,
10_Flags yes
Below is the code i am trying to get result, where i am kind of lost to extract the required information, please help me with this to extract information i am looking for
#include<iostream>
#include<string>
#include <fstream>
#include <cstring>
using namespace std;
bool findWord(string Filename, string const& find)
{
cout<<"Filename:"<<Filename<<"\t"<<"find:"<<find<<endl;
ifstream iFile(Filename.c_str());
if(!iFile)
{
cerr<<"File not opened!\n";
return false;
}
char c;
string content;
while(iFile.get(c) )
{
if(c != '\n')
{
content += c;
}
else
{
content = ""; //reset string after flag ',' was found
}
if(content == find)
return true;
}
cout<<"content:"<<content<<endl;
return false;
}
int main()
{
if(!findWord("abc.lst","Cable_pull1.sh"))
cout<<"failed"<<endl;
else
cout<<"success"<<endl;
return 0;
}
I would make use of std::getline
to read line by line in the file into a std::string
. You can then use the string
s member function find
to find what you're looking for.
When you've found Cable_pull1.sh
for example, you loop, again using std::getline
, and print the lines that follows until an empty line is found.
Example:
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
bool findWord(std::string Filename, std::string const& find) {
std::cout << "Filename:" << Filename << "\tfind:" << find << '\n';
std::ifstream iFile(Filename.c_str());
if(!iFile) {
std::cerr << "File not opened!\n";
return false;
}
std::string line;
while(std::getline(iFile, line)) { // read a whole line
// and find "find" in that line
std::size_t pos = line.find(find);
// if "find" is found (pos != std::string::npos),
// check that it's a full match to the rest of the line
if(pos != std::string::npos && line.substr(pos) == find) {
// ok, we found the correct entry in the file
// loop and print each line (except the "MH_TEST" lines)
while(std::getline(iFile, line)) {
if(line.size()==0) break; // empty line, break out
if(line.find("MH_TEST") == std::string::npos) {
// print lines not matching MH_TEST
std::cout << line << '\n';
}
}
return true;
}
}
return false;
}
int main() {
if(!findWord("abc.lst", "Cable_pull1.sh"))
std::cout << "failed\n";
else
std::cout << "success\n";
return 0;
}
Output:
Filename:abc.lst find:Cable_pull1.sh
DUAL_DOMAIN yes
CAMARO_STORAGE YES
success