Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2012
    Posts
    5

    Unanswered: How to get data from a file using contents of another file

    I have two files
    duplicate.txt
    original.log

    I want to get data from original.txt corresponding to the values that are present in duplicate.txt

    I tried

    grep -wf duplicate.txt original.txt >> f.txt

    but it doesn't give any output
    f.txt is empty

    Please help me

  2. #2
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    Code:
    while read line
    do
       #add some code here if you need to decode/parse the contents of the line.
       grep $line original.txt
    done <duplicate.txt
    Warning, this will read original.txt as many times as there lines in duplicate.txt.

    If you just want to know the differences between the two files use
    Code:
    diff original.txt duplicate.txt

  3. #3
    Join Date
    Jul 2012
    Posts
    5
    Thank You it was very helpful.

  4. #4
    Join Date
    Jul 2012
    Posts
    5
    I have multiple conditions to check.
    So i am using different loops to check each condition
    I have 3 arrays
    Code:
    Remitid
    Taskid
    comp
    I want to check file Sri1.log if it has the values stored in these arrays one by one. Means firstly for 1 value of Remitid it has 1st value of Taskid and then 1st value of comp array and then fetch value from array which has word task started

    The code I have written is


    Code:
    while var1 in "${Remitid[@]}"
    do
    	for var2 in "${Taskid[@]}"
    	do
    		for var3 in "${comp[@]}"
    		do
    			while read var4
    				grep 
    			done < Sri1.log
    		done
    	done
    done
    But i don't know what to write in "grep". How to use all variables along with "Task started"

    Sri1.log is file which is separated by pipe"|" operator. I want to fetch 1st column of the file if the condition is true.

    Please help

  5. #5
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    How many entries are there in each of the arrays?
    How many records (lines) are there in Sri1.log?
    Is there a relationship among the remitid, taskid and comp array entries that could limit the search?

    Your current code processes the log file (number_of_remitid_entries * number_of_task_id_entries * number_of_comp_entries) times.

    It would probably be better to process the log file once, and then examine each line to see if the correct conditions exist.

    Code:
    while read var4 
    do
    while var1 in "${Remitid[@]}"
    do
    	for var2 in "${Taskid[@]}"
    	do
    		for var3 in "${comp[@]}"
    		do
    	        	echo "$var4" |grep $var3 >/dev/null
                            c1=$?
                            echo "$var4"|grep $var2 >/dev/null
                            t1=$?
                            echo "$var4"|grep $var1 >/dev/null
                            r1=$?
                            #c1, t1, r1 are equal 0 if the grep is successful 
                            # and equal 1 if the grep is unsuccessful
                            #add code here to process the 8 possible results.
                            # if [ "$c1$t1$r1" = "000" ]  etc
    			done 
    		done
    	done
    done<Sri1.log

  6. #6
    Join Date
    Jul 2012
    Posts
    5
    The array Remitid has will have maximum 4-5 enteries
    Taskid will be 10-15 and Comp will be 40-50

    Yes each of these values have some relationship as
    Each value in remitid will have corresponding few values from taskid array and all the values from comp array

    Sri1.log is a very big file nearly 3000 lines or even more

  7. #7
    Join Date
    Jul 2012
    Posts
    5
    What does /dev/null stands for?

    Is it the destination file in which the word is to be searched?
    Because it gives an error
    Code:
    there is no such file and directory
    I have replaced this with the destination file name

  8. #8
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    wiki//dev/null
    The output of the grep command is discarded, as it is not required. If the result of the grep command is true, then a line that meets the test criteria has been found.
    If you only expect a small percentage of the lines in the log file to be affected, and this is a one time job, you could manually grep the log file for each remitid and create 4 or 5 separate files. Then search the resultant files for the other criteria.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •