LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Extracting rows from one file based on column entries in another file (https://www.linuxquestions.org/questions/linux-newbie-8/extracting-rows-from-one-file-based-on-column-entries-in-another-file-4175504112/)

mphillips67 05-06-2014 02:28 PM

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.

allend 05-06-2014 04:57 PM

This should be straightforward using grep with the -F and -f options i.e. 'grep -Ff File1 File2'.

mphillips67 05-06-2014 05:18 PM

Quote:

Originally Posted by allend (Post 5165951)
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.

allend 05-06-2014 06:26 PM

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.


All times are GMT -5. The time now is 02:00 PM.