LinuxQuestions.org
Visit Jeremy's Blog.
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, 02: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, 02:57 AM   #2
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,415

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

Welcome to LQ!

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

Original Poster
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, 02:24 AM   #4
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,415

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, 02:30 AM   #5
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,415

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, 07:43 AM   #6
gopinathnatarajan
LQ Newbie
 
Registered: Sep 2012
Location: Bangalore
Distribution: Red Hat, CentOs
Posts: 16
Blog Entries: 1

Original Poster
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, 08:06 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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, 03:24 AM   #8
gopinathnatarajan
LQ Newbie
 
Registered: Sep 2012
Location: Bangalore
Distribution: Red Hat, CentOs
Posts: 16
Blog Entries: 1

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 09:01 AM
Removed x11 common files galfly Linux - Desktop 6 08-16-2010 12:04 AM
How to change a common name in files? Drigo Linux - Newbie 3 10-29-2009 10:07 PM
comparing files..ouput which r not common fs11 Programming 1 06-25-2007 06:09 PM
Finding files and then finding content within those files... Maeltor Linux - Software 5 03-13-2007 01:06 PM


All times are GMT -5. The time now is 02:37 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration