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 > Shell - Search/Copy Record - Please Help

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-03-05, 14:29
old_farmer old_farmer is offline
Registered User
 
Join Date: Feb 2005
Posts: 19
Shell - Search/Copy Record - Please Help

Could someone please offer suggestions:

I am trying to search a textfile with hundreds of lines (records).

So I need to know how to search until the end of the file per each line. Within each line, I need to search for a particular string. If the string matches, I want to copy the whole line (record) into a new file (building a new file with the list of matching records).

Also, when I copy the record, I would like to place a 'insert into table(' statement at the beginning and then put a semicolon at the end so that I can execute the file as sql.

Please let me know if you have any ideas.

Thanks a lot!
Reply With Quote
  #2 (permalink)  
Old 02-03-05, 14:43
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
sed -ne '/string2search/s#.*#insert into table(&;#p' oldFile > newFile
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+

Last edited by vgersh99; 02-03-05 at 15:02.
Reply With Quote
  #3 (permalink)  
Old 02-03-05, 15:05
old_farmer old_farmer is offline
Registered User
 
Join Date: Feb 2005
Posts: 19
Quote:
Originally Posted by vgersh99
sed -e '/string2search/s#.*#insert into table(&;#g' oldFile > newFile
Thank you for your help. I tried the following but it copied all records to the new file:

sed -e '/MFP/s#.*#insert into table(&;#g' file1 > file2


The string that I am searching for is "MFP", the initial file is file1, and the new file is file2. Could you please tell me what I'm doing wrong. Thanks a lot!
Reply With Quote
  #4 (permalink)  
Old 02-03-05, 15:07
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
sorry for the confusion - I've just modified my original post iwth the correction.

Code:
sed -ne '/MFP/s#.*#insert into table(&;#p' file1 > file2
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+
Reply With Quote
  #5 (permalink)  
Old 02-03-05, 15:16
old_farmer old_farmer is offline
Registered User
 
Join Date: Feb 2005
Posts: 19
Quote:
Originally Posted by vgersh99
sorry for the confusion - I've just modified my original post iwth the correction.

Code:
sed -ne '/MFP/s#.*#insert into table(&;#p' file1 > file2
Wow, you’re a genius. It worked perfectly. It would have taken me forever to figure that out. Thanks a lot!
Reply With Quote
  #6 (permalink)  
Old 02-03-05, 19:33
old_farmer old_farmer is offline
Registered User
 
Join Date: Feb 2005
Posts: 19
Can you please tell me what I would do if I don't want to insert the statement: 'insert into table (' and if I don't want the semicolon at the end?
Reply With Quote
  #7 (permalink)  
Old 02-03-05, 20:38
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
P: Doctor, it hurts when I do THAT!
D: Then don't do THAT!

Code:
sed -ne '/MFP/s#.*#&#p' file1 > file2
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+
Reply With Quote
  #8 (permalink)  
Old 02-03-05, 21:10
old_farmer old_farmer is offline
Registered User
 
Join Date: Feb 2005
Posts: 19
Cool! Thanks a lot
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