LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   finding common between two files (https://www.linuxquestions.org/questions/linux-newbie-8/finding-common-between-two-files-4175428141/)

gopinathnatarajan 09-20-2012 01:23 AM

finding common between two files
 
I need to print common lines between two files using "FOR LOOP" and "IF ELSE" command.


#vi File1

apple
orange
mango
grapes


#vi File2

apple
orange
strawberry
banana



Output:

apple
orange



Please Help Me Out Of This Quest..!

Satyaveer Arya 09-20-2012 01:57 AM

Hi,

Welcome to LQ!

Code:

# comm file1 file2

gopinathnatarajan 09-21-2012 01:17 AM

Output:

apple
orange
banana
mango
grapes
strawberry



Hi thanks a lot for your reply...!! I used the command which you replied me but i got the above output..!!

Satyaveer Arya 09-21-2012 01:24 AM

Which OS are you using?

And I checked it on my test machine. It gives me this output -

Code:

[root@testmachine ~]# comm file1 file2
                apple
mango
comm: file 1 is not in sorted order
banana
grapes
        orange
        strawberry
comm: file 2 is not in sorted order
        banana

Code:

[root@testmachine ~]# comm --nocheck-order file1 file2
                apple
mango
banana
grapes
        orange
        strawberry
        banana


Satyaveer Arya 09-21-2012 01:30 AM

Here is the correct output. But the contents of the file should be in sorted order. Or you can first sort the contents of the file.

I have file1 and file2.

Code:

[root@testmachine ~]# cat file1
apple
banana
grapes
mango

Code:

[root@testmachine ~]# cat file2
apple
banana
orange
strawberry

Code:

[root@testmachine ~]# comm --nocheck-order file1 file2
                apple
                banana
grapes
mango
        orange
        strawberry

In this output the first column shows the remaining contents of file1 other than the similar ones. Second column shows the remaining output of file2 other than the similar ones. And the third column shows the output which is common in both the files.

gopinathnatarajan 09-23-2012 06:43 AM

Hi
 
I just mis-understand the command.. Now am getting the correct output.

Thank You so much Satyaveer Arya..!!

David the H. 09-23-2012 07:06 AM

1) This sounds quite a bit like homework to me. If so, please read the LQ rule regarding homework.

2) Please use ***[code][/code] tags*** around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

3) Please supply more detail and context. Do you actually NEED to use for/if for this? What have you tried to do so far? Also be sure to add any important information about the environment. Are you using bash or a different shell, for example?

4) Never just say it "didn't work". Always post the exact code you used and the error messages you got.

5) Have you tried searching the forums yet? This is the kind of question that comes up with frustrating regularity.

gopinathnatarajan 10-01-2012 02:24 AM

Thanks for your information David the H.


All times are GMT -5. The time now is 12:58 PM.