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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
10-28-2016, 09:25 AM
|
#1
|
Member
Registered: Oct 2016
Posts: 41
Rep: 
|
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.
|
|
|
10-28-2016, 09:32 AM
|
#2
|
LQ 5k Club
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,570
|
Welcome to LQ!
Have you looked at the 'join' command?
|
|
2 members found this post helpful.
|
10-28-2016, 09:37 AM
|
#3
|
LQ Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
|
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.
|
10-28-2016, 09:37 AM
|
#4
|
Senior Member
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,908
|
Or maybe the "comm" command?
|
|
|
10-28-2016, 11:27 AM
|
#5
|
Member
Registered: Oct 2016
Posts: 41
Original Poster
Rep: 
|
Thanks for the suggestions. It looks like join is what I've been looking for. Will test it out.
|
|
|
10-28-2016, 05:21 PM
|
#6
|
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
|
There's also diff and meld, a sort of GUI diff.
|
|
|
10-31-2016, 02:37 PM
|
#7
|
Member
Registered: Oct 2016
Posts: 41
Original Poster
Rep: 
|
It turned out join was the easiest solution, and gave me the wanted output.
Thanks for all suggestions.
|
|
|
11-01-2016, 01:04 PM
|
#8
|
LQ Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
|
Quote:
Originally Posted by Bartonsen
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.
|
|
|
All times are GMT -5. The time now is 05:39 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|