LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 04-06-2009, 10:29 AM   #1
pwd_pwd_omg_pwd
LQ Newbie
 
Registered: Oct 2008
Location: Cardiff, Wales
Distribution: Jaunty
Posts: 20

Rep: Reputation: 0
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
 
Old 04-06-2009, 12:04 PM   #2
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware, Mint
Posts: 1,240

Rep: Reputation: 196Reputation: 196
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
 
Old 04-06-2009, 12:50 PM   #3
john test
Member
 
Registered: Jul 2008
Distribution: ubuntu 9.10
Posts: 527
Blog Entries: 1

Rep: Reputation: 35
How does that get it into file3?
 
Old 04-06-2009, 12:58 PM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,503

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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.
 
Old 04-07-2009, 04:08 AM   #5
pwd_pwd_omg_pwd
LQ Newbie
 
Registered: Oct 2008
Location: Cardiff, Wales
Distribution: Jaunty
Posts: 20

Original Poster
Rep: Reputation: 0
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
 
Old 04-07-2009, 01:48 PM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,503

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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.
 
  


Reply

Tags
grep, line, matching, while


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
sed or grep : delete lines containing matching text raj000 Linux - General 18 09-08-2012 09:38 AM
Copy from file1 to file2 - Trying sed LinuxCrayon Linux - Newbie 3 03-30-2008 07:51 AM
[Comp] How to create a file from the differences between file1 and file2 Xeratul Linux - Software 2 11-19-2006 01:20 PM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM
Avoid cat file1>>file2 automatic add return after file2. AshesOfTime Programming 5 11-25-2004 07:27 AM


All times are GMT -5. The time now is 09:35 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration