Repository navigation
Expand file tree
/
Copy pathsux.cpp
More file actions
153 lines (129 loc) · 3.69 KB
/
Copy pathsux.cpp
File metadata and controls
153 lines (129 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
________ ______ ___
/ ___/ || \ \/ /
_\___ \| |/ /> <
/______>______//__/\_ \
\/ v1.01
written by Philippe Bourcier
with input from Serge Danzanvilliers
with fixes by Pierre Ynard
This code is published under the BSD License:
http://www.opensource.org/licenses/bsd-license.php
The main idea is to replace "... | sort | uniq -c [ | sort -(r)(n) ] [ | head -N ]"
This program is (way) faster and less memory consuming than the above commands.
Sample use:
# cat file.txt | awk ... | sort | uniq -c | sort -rn | head -n 20
will be replaced by:
# cat file.txt | awk ... | sux -rn 20
# cat file.txt | awk ... | sort | uniq -c | sort -n
will be replaced by:
# cat file.txt | awk ... | sux -s
# cat file.txt | awk ... | sort | uniq -c
will be replaced by:
# cat file.txt | awk ... | sux
# cat file.txt | awk ... | sort | uniq -c | head -n 20
will be replaced by:
# cat file.txt | awk ... | sux -n 20
*/
#include <algorithm>
#include <string>
#include <iostream>
#include <fstream>
#include <map>
#include <vector>
#include <stdlib.h>
#include <unistd.h>
using namespace std;
typedef pair<std::string, int> mypair;
struct intcmp {
bool operator()(const mypair &lhs, const mypair &rhs) {
return lhs.second < rhs.second;
}
};
struct rintcmp {
bool operator()(const mypair &lhs, const mypair &rhs) {
return lhs.second > rhs.second;
}
};
void usage(string p) {
cout << "Usage: " << p << " [OPTION]... [FILE]..." << endl <<
"Write sorted concatenation of all FILE(s) to standard output." << endl << endl <<
"Ordering options:" << endl <<
"-s sort the result (sort -n)" << endl <<
"-r reverse sorting order (sort -rn)" << endl <<
"-nN limit output to the first N lines" << endl << endl <<
"Report bugs to <philippe@sysctl.org>." << endl; }
int main(int argc, char *argv[]) {
map<string,int> c;
string line;
bool s, r, l;
s = r = l = false;
char *n=NULL;
int opt;
int z=-1;
// Getting arguments
while ((opt=getopt(argc,argv,"hsrn:"))!=-1) {
switch (opt) {
case 's':
s = true;
break;
case 'r':
r = true;
break;
case 'n':
l = true;
n = optarg;
z=atoi(n);
break;
case 'h':
usage(argv[0]);
return 0;
case '?':
cout << "Try '" << argv[0] << " -h' for more information." << endl;
return 1;
default:
return 0;
}
}
if(argv[optind]) {
ifstream file(argv[optind]);
if(!file.is_open()) {
cout << "Unable to open file : " << argv[(argc-1)] << endl;
return 1;
}
while(file.good()) {
getline(file,line);
if (!file.eof()) c[line]++;
}
file.close();
} else {
if(!cin) return 1;
while(cin) {
getline(cin,line);
if (!cin.eof()) c[line]++;
}
}
// Sorting
if(s || r) {
vector<mypair> myvec(c.begin(),c.end());
if(r) partial_sort(myvec.begin(),myvec.begin()+c.size(),myvec.end(),rintcmp());
else partial_sort(myvec.begin(),myvec.begin()+c.size(),myvec.end(),intcmp());
for (int i=0;i<int(c.size());++i) {
cout << myvec[i].second << " " << myvec[i].first << endl;
if(l) {
z--;
if(z==0) break;
}
}
} else {
// No Sorting
for (map<string,int>::iterator i=c.begin();i!=c.end();i++) {
cout << i->second << " " << i->first << endl;
if(l) {
z--;
if(z==0) break;
}
}
}
return 0;
}