LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-20-2012, 01:23 AM   #1
gopinathnatarajan
LQ Newbie
 
Registered: Sep 2012
Location: Bangalore
Distribution: Red Hat, CentOs
Posts: 16
Blog Entries: 1

Rep: Reputation: Disabled
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..!
 
Old 09-20-2012, 01:57 AM   #2
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,420

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Hi,

Welcome to LQ!

Code:
# comm file1 file2
 
Old 09-21-2012, 01:17 AM   #3
gopinathnatarajan
LQ Newbie
 
Registered: Sep 2012
Location: Bangalore
Distribution: Red Hat, CentOs
Posts: 16

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
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..!!
 
Old 09-21-2012, 01:24 AM   #4
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,420

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
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
 
Old 09-21-2012, 01:30 AM   #5
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,420

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
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.
 
Old 09-23-2012, 06:43 AM   #6
gopinathnatarajan
LQ Newbie
 
Registered: Sep 2012
Location: Bangalore
Distribution: Red Hat, CentOs
Posts: 16

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Hi

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

Thank You so much Satyaveer Arya..!!
 
Old 09-23-2012, 07:06 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
1 members found this post helpful.
Old 10-01-2012, 02:24 AM   #8
gopinathnatarajan
LQ Newbie
 
Registered: Sep 2012
Location: Bangalore
Distribution: Red Hat, CentOs
Posts: 16

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Thanks for your information David the H.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
pam common- files arizonagroovejet Linux - General 2 07-29-2011 08:01 AM
Removed x11 common files galfly Linux - Desktop 6 08-15-2010 11:04 PM
How to change a common name in files? Drigo Linux - Newbie 3 10-29-2009 09:07 PM
comparing files..ouput which r not common fs11 Programming 1 06-25-2007 05:09 PM
Finding files and then finding content within those files... Maeltor Linux - Software 5 03-13-2007 12:06 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration