LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-05-2008, 10:18 AM   #1
laopi
LQ Newbie
 
Registered: Nov 2008
Posts: 9

Rep: Reputation: 0
pattern matching question


Hi,

I have a pattern file,

1111
2222
4444

and another working file

1111,abc
2222,dce
3333,ert
4444,dfg
5555,tuy

I only want the lines

1111,abc
2222,dce
4444,dfg

I can cat the working file line by line, but it's really slow, because my working file is huge, any idea?

peter
 
Old 11-05-2008, 10:30 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Here are 2 that came to mind, there are other ways of doing it, but I think these are the best (speed wise).

Using egrep: egrep "1111|2222|4444" infile
Using sed: sed -n '/\(1111\|2222\|4444\)/p' infile

I think that the egrep version will be fastest, it does depend a bit how big the infile is.

Hope this helps.
 
Old 11-05-2008, 10:51 AM   #3
laopi
LQ Newbie
 
Registered: Nov 2008
Posts: 9

Original Poster
Rep: Reputation: 0
my pattern file is big too, I think the command line will be oversized.
 
Old 11-05-2008, 10:57 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

That is an assumption or a fact?
 
Old 11-05-2008, 11:08 AM   #5
laopi
LQ Newbie
 
Registered: Nov 2008
Posts: 9

Original Poster
Rep: Reputation: 0
my current pattern file is 5238 lines and the size is 240336.
 
Old 11-05-2008, 11:11 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

grep -f pattern.file in.file

One pattern per line in the pattern.file.
 
Old 11-05-2008, 11:34 AM   #7
laopi
LQ Newbie
 
Registered: Nov 2008
Posts: 9

Original Poster
Rep: Reputation: 0
thanks, I think that's what I want

it's running some mins. ago, not finished, but I'm pretty sure that's what I want.

peter
 
  


Reply

Tags
matching



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
need help with perl - pattern matching johngreg Programming 5 10-13-2008 01:08 PM
pattern matching nadeemr Linux - Newbie 8 06-13-2007 11:05 AM
Sed pattern matching digitalbrutus Programming 1 08-20-2006 01:37 PM
pattern matching question - grep cbriscoejr Programming 1 02-09-2006 08:30 PM
Perl Pattern Matching Question pete1234 Programming 2 08-27-2005 10:26 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:11 AM.

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