Hey im new in this..

I want to search the user input in file (by lines) but not all then with this line search on another file ( with the specific line) and show to the user.

Example:

file1.txt
=======
a
aa
aaa
aab
aac


file2.txt (corresponding md5 hashes of every text line)
======
0cc175b9c0f1b6a831c399e269772661
4124bc0a9335c27f086f24ba207a4912
47bce5c74f589f4867dbd57e9ca9f808
e62595ee98b585153dac87ce1ab69c3c
a9ced3dad556814ed46042de696e1849

========
Lets supposed to the user enter (want to crack) this hash: a9ced3dad556814ed46042de696e1849

im using

#!/bin/bash
linenum=$( grep -w -n a9ced3dad556814ed46042de696e1849 file2.txt | cut -f1 -d: )
sed -n "$linenum p" file1.txt

And works!

BUTTTT

Im using this commands in C with Pipes.. Here the code ...

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(){
    FILE *read_fp;
	int chars_read; 
        char searchash[1024] = "linenum=$(grep -n -w ";
	char buffer[BUFSIZ + 1]; 
	memset(buffer, '0', sizeof(buffer));  

	printf("Enter MD5 Hash: ");
	char hash;
	scanf("%s", &hash);

	strcat(searchash, &hash);
	strcat(searchash, " file2.txt | cut -f1 -d: )");


	read_fp = popen (searchash, "w");

	if (read_fp != NULL) {

		chars_read = fread(buffer, sizeof(char), BUFSIZ, read_fp);

   pclose(read_fp);
}
    system("sed -n \"$linenum p\" file1.txt");
}

...anything will be helpful

Greetings.

Recommended Answers

All 4 Replies

See if this helps. BUFSIZ was not what you think. I replaced it with a hardcode value, changed it if you like.

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(){
    FILE *read_fp;
    FILE *read_sed;
	int chars_read; 
        char command[1024] = "grep -n -w ";
	char buffer[1024 + 1]; 
	memset(buffer, 0, sizeof(buffer));  

	printf("Enter MD5 Hash: ");
	char hash[500];
	scanf("%s", hash);

	strcat(command, hash);
	strcat(command, " file2.txt | cut -f1 -d:");

        /*printf("command: %s\n",command);*/
	read_fp = popen (command, "r");

    if (read_fp != NULL) {
       while ((chars_read = fread(buffer, sizeof(char),1024 , read_fp)) > 0 ){
          /*printf("buf: %s\n",buffer);*/
          sprintf(command,"sed -n \"%d p\" file1.txt",atoi(buffer));
          /*printf("command: %s\n",command);*/
	  read_sed = popen (command, "r");
          if ((chars_read = fread(buffer, sizeof(char), 1024, read_sed)) > 0 ) {
               printf("%s -> %s",hash,buffer);
          }
          pclose(read_sed);
       }
       pclose(read_fp);
    }
    return 0;
}

It Works Thanks a lot for your help!

Greetings.

If you ever get more than one result from the grep command, you will want to change the fread to fgets. Right now it might not work too well if two lines come back.
If you sure this can't happen, you should change it anyway.
I should have posted it with fgets.

If you ever get more than one result from the grep command, you will want to change the fread to fgets. Right now it might not work too well if two lines come back.
If you sure this can't happen, you should change it anyway.
I should have posted it with fgets.

yep it's true I saw some garbage... I changed it thanks a lot!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.