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, 09:25 AM   #1
Bartonsen
Member
 
Registered: Oct 2016
Posts: 41

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, 09:32 AM   #2
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,570

Rep: Reputation: 2845Reputation: 2845Reputation: 2845Reputation: 2845Reputation: 2845Reputation: 2845Reputation: 2845Reputation: 2845Reputation: 2845Reputation: 2845Reputation: 2845
Welcome to LQ!

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

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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 11:48 AM.
 
1 members found this post helpful.
Old 10-28-2016, 09:37 AM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,908

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Or maybe the "comm" command?
 
Old 10-28-2016, 11:27 AM   #5
Bartonsen
Member
 
Registered: Oct 2016
Posts: 41

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, 05:21 PM   #6
frankbell
LQ Guru
 
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Ubuntu MATE, Mageia, and whatever VMs I happen to be playing with
Posts: 19,889
Blog Entries: 28

Rep: Reputation: 6345Reputation: 6345Reputation: 6345Reputation: 6345Reputation: 6345Reputation: 6345Reputation: 6345Reputation: 6345Reputation: 6345Reputation: 6345Reputation: 6345
There's also diff and meld, a sort of GUI diff.
 
Old 10-31-2016, 02:37 PM   #7
Bartonsen
Member
 
Registered: Oct 2016
Posts: 41

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, 01:04 PM   #8
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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 05:22 AM
Comparing 2 files ravi_nandula Linux - Newbie 7 07-09-2012 12:39 PM
[SOLVED] Comparing two files p3rcy Programming 10 02-14-2012 12:27 PM
comparing files newbiesforever Linux - Software 3 07-07-2010 03:20 PM
Comparing 2 Files xianzai Programming 2 05-23-2004 11:50 AM

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

All times are GMT -5. The time now is 05:39 PM.

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