Util: Add delimiter support to the cut function

This commit is contained in:
Riyyi
2021-10-02 12:14:14 +02:00
parent 22082a515b
commit a04b5232f4
2 changed files with 6 additions and 6 deletions
+5 -5
View File
@@ -133,22 +133,22 @@ System System::operator||(System rhs)
return rhs;
}
// cut -f
System& System::cut(uint32_t field)
// cut -f -d
System& System::cut(uint32_t field, char delimiter)
{
exec();
return apply([&field](std::vector<std::string>& lines) {
return apply([&field, &delimiter](std::vector<std::string>& lines) {
for (auto& line : lines) {
size_t count = 1;
size_t index = 0;
while (index != std::string::npos) {
if (count == field) {
line = line.substr(0, line.find_first_of(" "));
line = line.substr(0, line.find_first_of(delimiter));
break;
}
index = line.find_first_of(" ");
index = line.find_first_of(delimiter);
line = line.substr(index + 1);
count++;
}
+1 -1
View File
@@ -36,7 +36,7 @@ public:
System operator&&(System rhs);
System operator||(System rhs);
System& cut(uint32_t field);
System& cut(uint32_t field, char delimiter = '\t');
System& sort(bool unique = false);
System& tail(int32_t number, bool starting = false);
System& apply(LineCallback callback);