If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Unixware script question.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-12-03, 10:52
msb2112 msb2112 is offline
Registered User
 
Join Date: Sep 2003
Posts: 9
Unixware script question.

Hi,

This is probably a simple question:

I have two files, "filea", and "fileb". "filea" contains a list of customer numbers only, one in each record.

1234
1241
1250
4569
6433
etc.

"fileb" is a larger file containing the "complete" list of customer data with many more records in it, of which "filea" is a subset, that contains address info, phone, etc.

I want to use the list of customer numbers in "filea" to extract those customer's data from "fileb" and output the result into "filec".

I thought I could do something really simple like:

grep `cat filea` fileb > filec

but that didn't work. Any ideas?

Thanks, Mike.
Reply With Quote
  #2 (permalink)  
Old 09-12-03, 11:45
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Re: Unixware script question.

Quote:
grep `cat filea` fileb > filec
You were on the right track.

Use the -f flag with grep to pass in a list of patterns from a file.

e.g.

grep -f filea fileb > filec


Just so you understand why your effort wouldn't work...

`cat filea` would expand to for example:

customer1
customer2
customer3

... your grep command would therefore be interpreted by the shell as being:

grep customer1 customer2 customer3 fileb > filec

The above would look for the pattern "customer1" in the files "customer2", "customer3" and "fileb". The output would be redirected to "filec". You would get errors in filec because the files you have stipulated do not exist.

HTH

Last edited by Damian Ibbotson; 09-12-03 at 11:51.
Reply With Quote
  #3 (permalink)  
Old 09-12-03, 11:59
msb2112 msb2112 is offline
Registered User
 
Join Date: Sep 2003
Posts: 9
Re: Unixware script question.

Very nice... Thank you, this worked like a gem!


Quote:
Originally posted by Damian Ibbotson
You were on the right track.

Use the -f flag with grep to pass in a list of patterns from a file.

e.g.

grep -f filea fileb > filec


Just so you understand why your effort wouldn't work...

`cat filea` would expand to for example:

customer1
customer2
customer3

... your grep command would therefore be interpreted by the shell as being:

grep customer1 customer2 customer3 fileb > filec

The above would look for the pattern "customer1" in the files "customer2", "customer3" and "fileb". The output would be redirected to "filec". You would get errors in filec because the files you have stipulated do not exist.

HTH
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On