Sundown G 0 Newbie Poster

I post this thread in shell scripting too

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.

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.