LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 05-06-2014, 02:28 PM   #1
mphillips67
LQ Newbie
 
Registered: Jul 2013
Posts: 9

Rep: Reputation: Disabled
Extracting rows from one file based on column entries in another file


Hi,

I currently have two files one that looks like this (File1):

Code:
  
2R 123
2R 1678
3R 12
3R 1231
.. ..
and the other (File2) looks like this:

Code:
2R 10 34 12 21 23
2R 12 88 82 11 44  
2R 44 10 21 31 44 
2R 123 56 11 23 44
2R 555 13 11 33 55
2R 1678 12 22 77 33
3R 12 33 18 92 11
3R 1231 44 22 11 22
3R 2000 11 99 22 13
....................
I need too extract all of the rows from File2 where the first two columns have entries matching those in File1.

So, based on the example I have here, my output would look like:

Code:
2R 123 56 11 23 44
2R 1678 12 22 77 33
3R 12 33 18 92 11
3R 1231 44 22 11 22
I was able to find ways of doing similar if say File1 only had one column and I was only extracting rows from File2 based on that single column. But I haven't had any luck translating that to when I need to extract rows based on 2 columns.
 
Old 05-06-2014, 04:57 PM   #2
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
This should be straightforward using grep with the -F and -f options i.e. 'grep -Ff File1 File2'.
 
Old 05-06-2014, 05:18 PM   #3
mphillips67
LQ Newbie
 
Registered: Jul 2013
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by allend View Post
This should be straightforward using grep with the -F and -f options i.e. 'grep -Ff File1 File2'.
I gave that a try, but it didn't work. The output it generated was just blank. Problem seems to be with portions of File1 that only have a number in column one. For instance,

Quote:
4 3132
4 312313
4 231
It does seems to work for the rows in File1 where column 1 has a number and a letter.

Last edited by mphillips67; 05-06-2014 at 05:24 PM.
 
Old 05-06-2014, 06:26 PM   #4
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
Have File1 and File2 been generated within Windows? Perhaps you need to run the files through a utility like fromdos to convert any CRLF pairs to LF only.
 
  


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
[SOLVED] Sorting a file based on column number ramkisak Linux - Newbie 5 07-31-2013 02:16 AM
how to find match between two rows of column in same file using PERL genetist Linux - Newbie 3 07-13-2013 11:17 AM
[SOLVED] Delete rows based on values in a column using sed captainentropy Linux - Newbie 6 01-19-2011 08:59 AM
Text file manipulation: Extracting specific rows according to numerical pattern CHARL0TTE Linux - Newbie 3 10-07-2009 07:14 AM
Parsing rows and column data from a file using perl dav_y2k Programming 1 10-08-2006 11:57 AM

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

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