Skip to content

Instantly share code, notes, and snippets.

@judotens
Last active December 29, 2015 16:29
Show Gist options
  • Save judotens/7697871 to your computer and use it in GitHub Desktop.
Save judotens/7697871 to your computer and use it in GitHub Desktop.
Find an insensitive string in big text file. faster than cat and grep. supports multiple keywords, separated by space. no regex support.
/* cari string di file, case insensitive
support multiple keyword, tapi bukan regex. diisah by spasi aja
find an insensitive string in big raw text. faster than cat and grep.
supports multiple keywords, separated by space. no regex support.
@judotens
./cari bigfile.txt keyword
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main(int argc, char *argv[])
{
int i;
FILE *fp;
char * line = NULL;
size_t len = 0;
size_t read;
if (argc == 1)
{
printf("Cara pakai: %s <file> <keyword_1> <keyword_2> <keyword_n>\n", argv[0]);
exit(EXIT_SUCCESS);
}
fp=fopen(argv[1],"r");
if (fp == NULL)
exit(EXIT_FAILURE);
while ((read = getline(&line, &len, fp)) != -1)
{
for(i=2; i < argc; i++)
{
if (strcasestr(line,argv[i]))
{
printf("%s", line);
}
}
}
if (line)
free(line);
fclose(fp);
exit(EXIT_SUCCESS);
}
# standar cat & grep
$ time cat ~/testing.txt | grep -i gw | head -n 2
1319734881 djaycoholyc TweetCaster for Android Makasar Yang ga terpuaskan sama #memetwit gw itu si penulis Jakarta Underkompor si @arhamkendari lagi cerita juga. Intronya bangke XD
1319735029 purnamasyarii ÃœberSocial for BlackBerry Jakarta @arilupus amiinn, eh ri, lu msih punya file2 lab PSBD kaga ? Mau dong gw :D
real 0m0.027s
user 0m0.025s
sys 0m0.007s
# using this tools
$ time ./cari ~/testing.txt gw | head -n 2
1319734881 djaycoholyc TweetCaster for Android Makasar Yang ga terpuaskan sama #memetwit gw itu si penulis Jakarta Underkompor si @arhamkendari lagi cerita juga. Intronya bangke XD
1319735029 purnamasyarii ÃœberSocial for BlackBerry Jakarta @arilupus amiinn, eh ri, lu msih punya file2 lab PSBD kaga ? Mau dong gw :D
real 0m0.006s
user 0m0.004s
sys 0m0.003s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment