LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   extract matching lines from File2 based on File1. grep+while? (https://www.linuxquestions.org/questions/linux-newbie-8/extract-matching-lines-from-file2-based-on-file1-grep-while-717205/)

pwd_pwd_omg_pwd 04-06-2009 10:29 AM

extract matching lines from File2 based on File1. grep+while?
 
Hey guys and girls.
Im taking my first steps out into the world of perling, but i think ive jumped in too fast!

Im trying to extract matching lines from File2 based on a list in File1 (and then just paste it into File3)

Formats:
File1
Code:

GP_MASA_01F04_c
GP_MASA_33B06_c
GP_MASA_24D04_c
GP_MASA_19B08_c
GP_MASA_43C06_c

File2
Code:

GP_MASA_01F04_c,544213...etc
GP_MASA_38C02_c,43621...etc
GP_MASA_33B06_c,3253...etc
GP_MASA_43C06_c,12524...etc
GP_MASA_32B06_c,57432...etc
GP_MASC_05A08_c,24236...etc
GP_MASC_02A12_c,23432...etc
GP_MASC_01F04_c,232346...etc


I know what Im doing grep wise, so then put a while loop around it yeah?
Any help would be much appreciated!
Dan

w1k0 04-06-2009 12:04 PM

You don't need Perl for that task -- instead use bash and grep:

for line in `cat "File 1"` ; do grep $line "File 2" ; done

john test 04-06-2009 12:50 PM

How does that get it into file3?

colucix 04-06-2009 12:58 PM

Why to not simply use the -f option of grep?
Code:

grep -f file1 file2 > file3
using -f the lines of file1 as interpreted as patterns to be searched into file2. No need to loop over file1, since grep -f do it for you. See man grep for future reference.

pwd_pwd_omg_pwd 04-07-2009 04:08 AM

cheers for all the replies guys. didnt realise i could use a txt list as an input into grep. but still, using your:
Code:

grep -f file1 file2 > file3
doesnt work for me. Its just pasting the entire contents of file2 into file3.
(btw, im using the -v flag because i want all the lines that arent in the file1 deffinition file)
so now my cmd looks:
Code:

grep -v -f file1 file2 > file3
does anyone know why it would be doing this? its driving my mad

colucix 04-07-2009 01:48 PM

I noticed in the File 1 you pasted in your previous post, you had a trailing blank space at the end of each line. If this is the case, try to remove them. I reproduced your situation and it works quite well for me.


All times are GMT -5. The time now is 09:02 AM.