Help to filter read through reference file by using c language


 
Thread Tools Search this Thread
Top Forums Programming Help to filter read through reference file by using c language
# 1  
Old 07-25-2011
Help to filter read through reference file by using c language

Input_file
Code:
#tmp_2
werweraewghe
@tmp_2
123sdfs57a
#tmp_1
aewrgheheghe
@tmp_1
457awrerfa87
#tmp_4
trtyrghe
@tmp_4
8898rtyr2

reference_file
Code:
#tmp_4
#tmp_1

output_file
Code:
#tmp_4
trtyrghe
@tmp_4
8898rtyr2
#tmp_1
aewrgheheghe
@tmp_1
457awrerfa87

Logic thinking to solve the problem:
Code:
1. Based on the record of reference_file, print out those content that appear in Input_file into another output_file

Thanks for any advice.
# 2  
Old 07-25-2011
Just translate this to C Smilie
Code:
while read s; do
  while read t; do
    if echo "$t" | grep "${s#\#}"; then
      read q; echo "$q"
    fi
  done <Input_file
done <reference_file
#tmp_4
trtyrghe
@tmp_4
8898rtyr2
#tmp_1
aewrgheheghe
@tmp_1
457awrerfa87

This User Gave Thanks to yazu For This Post:
# 3  
Old 07-25-2011
Try:
Code:
awk 'NR==FNR{sub("#","");a[$0]=1;next}/^#|@/{x=$1;sub("#|@","",x);if (x in a){print;getline;print}}' ref_file in_file

This User Gave Thanks to bartus11 For This Post:
# 4  
Old 07-25-2011
But if seriously what did you try? Or do you just want somebody write a program for you?
# 5  
Old 07-25-2011
Hi yazu,
I'm edited my source code now.
Will update to this thread soon.
Sorry for delay.

---------- Post updated at 03:42 AM ---------- Previous update was at 02:24 AM ----------

Hi yazu,
this is the c code that I tried:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char**argv)
{
    FILE *fh, *fh1, *fout;
    char buf[4096];
    char buf1[4096];

    if (argc != 3)
    {
        printf("Usage: %s <input file> <reference file> <output file>\n", argv[0]);
        return -1;
    }

    if ((fh = fopen(argv[1], "r")) == NULL)
    {
        perror("fopen");
        return -1;
    }
    if ((fh1 = fopen(argv[2], "r")) == NULL)
    {
        perror("fopen");
        return -1;
    }
    if ((fout = fopen(argv[3], "w")) == NULL)
    {
        perror("fopen");
        return -1;
    }


       while(fgets(buf1, sizeof(buf1), fh1))
       {
               if(buf[0]== '#')
               {
                       buf[0]='#';
                       fputs(buf1, fout);
                       if(fgets(buf, sizeof(buf), fh) ) continue;
                       fputs(buf, fout);
               }
       }
       fclose(fh);
       fclose(fh1);
       fclose(fout);
    return 0;


}

It can't do well Smilie
Seems like wrong logic during the middle part of the code.
Thanks for any advice.
# 6  
Old 07-25-2011
Ok. Let's try.
1. Rename variables to work easier, For example fh - fref, fh1 - fsrc, buf - ref, buf1 - src.
2. You need two loops - see my shell prototype.
3. You need strings, not char buffers, so after reading add '\0' to the end of string (you should remember the value from fgets - use auxiliary variable).
4. You need remove the first char from ref and src strings - use pointers (it's better to use two more variables).
5. Use strcmp from standard library to compare strings (find what header you need).
6. If there is a match, print src string and get and print the next string.

But before starting to code try to write the algorithm in words or (better) in pseudocode.

I believe you can to write a good program - you have the good start. If you would have some difficulties, try to write some simple programs at first - for example - the program which will be read one line from reference file, remove the first char, print it, and then print all lines from input file. Or the program which looks for constant string ("#tmp4") in the input file and print matching lines. Then improve it - it should print the matching and the next lines. And so on.

PS Sorry for my English.

Last edited by yazu; 07-25-2011 at 06:28 AM..
# 7  
Old 07-25-2011
Quote:
Originally Posted by yazu
3. You need strings, not char buffers, so after reading add '\0' to the end of string (you should remember the value from fgets - use auxiliary variable).
If fgets didn't null-terminate for you, how could you possibly know where the NULL belongs? fgets terminates for you, like most string functions.
These 2 Users Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

awk to replace values in one file using a second reference file

Hi, I'd be grateful for your help with the following: I have a file with a single column (file1). Let's say the values are: a b c 5 d I have a second, reference file (ref_file), which is colon-delimited, and is effectively a key. Let's say the values in it are: a:1 b:2 c:3 d:4... (4 Replies)
Discussion started by: aberg
4 Replies

2. Shell Programming and Scripting

Keeping record of file 2 based on a reference file 1 in awk

I have 2 input files (tab separated): file1: make_A 1990 foo bar make_B 2010 this that make_C 2004 these those file2: make_X 1970 1995 ref_1:43 ref_2:65 make_A 1970 1995 ref_1:4 ref_2:21 ref_3:18 make_A 1980 2002 ref_1:7 ref_2:7 ref_3:0 ... (2 Replies)
Discussion started by: beca123456
2 Replies

3. UNIX for Dummies Questions & Answers

Filter records in a huge text file from a filter text file

Hi Folks, I have a text file with lots of rows with duplicates in the first column, i want to filter out records based on filter columns in a different filter text file. bash scripting is what i need. Data.txt Name OrderID Quantity Sam 123 300 Jay 342 498 Kev 78 2500 Sam 420 50 Vic 10... (3 Replies)
Discussion started by: tech_frk
3 Replies

4. UNIX for Dummies Questions & Answers

Shell script to read lines in a text file and filter user data Shell Programming and Scripting

sxsaaas (3 Replies)
Discussion started by: VikrantD
3 Replies

5. Shell Programming and Scripting

Perl de-reference code reference variable

Guys, May i know how can we de reference the code reference variable.? my $a = sub{$a=shift;$b=shift;print "SUM:",($a+$b),"\n";}; print $a->(4,5); How can we print the whole function ? Please suggest me regarding this. Thanks for your time :) Cheers, Ranga :) (0 Replies)
Discussion started by: rangarasan
0 Replies

6. Ubuntu

Any way we can create an SMTP server and use any scripting language to read emails from that server

Is there any way to create an SMTP mail server will all granular permissions to it so that I can read emails which that server receives through any scripting language and also reply from the same server automatically? (3 Replies)
Discussion started by: sandeepcm
3 Replies

7. UNIX for Dummies Questions & Answers

awk language reference: book

Gurus, I am looking for a book on Awk programming. A quick Google search gave me Amazon.com: The AWK Programming Language (9780201079814): Alfred V. Aho, Brian W. Kernighan, Peter J. Weinberger: Books by Alfred Aho. Is there anything other than that? Any advice? Thanks. Al. (1 Reply)
Discussion started by: alan
1 Replies

8. Shell Programming and Scripting

Shell script to read lines in a text file and filter user data

hi all, I have this file with some user data. example: $cat myfile.txt FName|LName|Gender|Company|Branch|Bday|Salary|Age aaaa|bbbb|male|cccc|dddd|19900814|15000|20| eeee|asdg|male|gggg|ksgu|19911216||| aara|bdbm|male|kkkk|acke|19931018||23| asad|kfjg|male|kkkc|gkgg|19921213|14000|24|... (4 Replies)
Discussion started by: srimal
4 Replies

9. Programming

How to read the CTS and DSR of RS232 in Unix using C language?

Hello to all Gurus out there, Could you show me a source code in Unix platform using C language. I want to read the status or voltage level of the DSR and CTS. Thanks a lot, Swing5 (2 Replies)
Discussion started by: Swing5
2 Replies

10. UNIX for Dummies Questions & Answers

Read from a file then filter the output

Hi i need a script which reads a certain file and then display it on the screen. The thing is i need to filter the display output. this is an example of the file which is to be loaded in the script. asdfg1.1.1|98 hjkldfe4.0.3|123 asdzxzvdweradfsdafascv10.0.10|123456789... (1 Reply)
Discussion started by: khestoi
1 Replies
Login or Register to Ask a Question