LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 10-28-2016, 10:25 AM   #1
Bartonsen
LQ Newbie
 
Registered: Oct 2016
Posts: 3

Rep: Reputation: Disabled
Comparing two files


I have two files which I want to compare, and display the common.

file1:
0010
0010
0010
1213
7010
7010
8732
8732
8732

file2:
0010 hello
0020 world
0130 this is
0140 just some
1213 randow text
1214 to prove that
1215 someone can
7010 fix
7019 my issue
8732 thanks in
9732 advance

The output should be:
0010 hello
0010 hello
0010 hello
1213 randow text
7010 fix
7010 fix
8732 thanks in
8732 thanks in
8732 thanks in

I have tried with "grep -f file1 file2" but the output shows only 1 line per match:
0010 hello
1213 randow text
7010 fix
8732 thanks in

The first word is always a 4 digit word in both files.
I want to print the line from file2 each time there is a match from the word in file1.
 
Old 10-28-2016, 10:32 AM   #2
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,429

Rep: Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348
Welcome to LQ!

Have you looked at the 'join' command?
 
2 members found this post helpful.
Old 10-28-2016, 10:37 AM   #3
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,577
Blog Entries: 14

Rep: Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969
You could do it with a while loop:

Code:
while read val
do grep $val file2
done <file1
or explore the join command. "man join"

Last edited by MensaWater; 10-28-2016 at 12:48 PM.
 
1 members found this post helpful.
Old 10-28-2016, 10:37 AM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,599

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Or maybe the "comm" command?
 
Old 10-28-2016, 12:27 PM   #5
Bartonsen
LQ Newbie
 
Registered: Oct 2016
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks for the suggestions. It looks like join is what I've been looking for. Will test it out.
 
Old 10-28-2016, 06:21 PM   #6
frankbell
LQ Guru
 
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Debian, Mint, OpenBSD
Posts: 11,333
Blog Entries: 12

Rep: Reputation: 2729Reputation: 2729Reputation: 2729Reputation: 2729Reputation: 2729Reputation: 2729Reputation: 2729Reputation: 2729Reputation: 2729Reputation: 2729Reputation: 2729
There's also diff and meld, a sort of GUI diff.
 
Old 10-31-2016, 03:37 PM   #7
Bartonsen
LQ Newbie
 
Registered: Oct 2016
Posts: 3

Original Poster
Rep: Reputation: Disabled
It turned out join was the easiest solution, and gave me the wanted output.
Thanks for all suggestions.
 
Old 11-01-2016, 02:04 PM   #8
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,577
Blog Entries: 14

Rep: Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969
Quote:
Originally Posted by Bartonsen View Post
It turned out join was the easiest solution, and gave me the wanted output.
Thanks for all suggestions.
Please go to thread tools and marked this is as Solved. Also you might want to post the exact usage of join you used so others finding this thread in future can see it in light of your example files.
 
  


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
Comparing two files. ted_chou12 Linux - Software 1 09-29-2012 06:22 AM
Comparing 2 files ravi_nandula Linux - Newbie 7 07-09-2012 01:39 PM
[SOLVED] Comparing two files p3rcy Programming 10 02-14-2012 01:27 PM
comparing files newbiesforever Linux - Software 3 07-07-2010 04:20 PM
Comparing 2 Files xianzai Programming 2 05-23-2004 12:50 PM


All times are GMT -5. The time now is 12:55 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