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 script to SEARCH multiple string occurances

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-18-04, 20:39
gomes009 gomes009 is offline
Registered User
 
Join Date: Feb 2004
Posts: 37
Shell script to SEARCH multiple string occurances

I need to search the strings in a text file that has the following information:


INSERT INTO TEST123
values (1,2,3);

INSERT INTO ABCD
values (1,2,3);

UPDATE TEST123
SET
ABC='123'
WHERE ABC='124';

UPDATE ABCD
SET
ABC='123'
WHERE ABC='124';

DELETE FROM TEST123
WHERE ABC = '111'
AND DEF= '222'
AND GHI='333';


DELETE FROM ABCD
WHERE ABC = '111'
AND DEF= '222'
AND GHI='333';

How could I Search for the string "TEST123" and then search for the following character ";".

Once these two strings are found, I want to Copy the data from line1(FIRST STRING FOUND) and lineN(SECOND STRING FOUND) to another file..


NEW FILE OUTPUT:


INSERT INTO TEST123
values (1,2,3);

UPDATE TEST123
SET
ABC='123'
WHERE ABC='124';

DELETE FROM TEST123
WHERE ABC = '111'
AND DEF= '222'
AND GHI='333';


Any help is appreciated
Reply With Quote
  #2 (permalink)  
Old 11-23-04, 16:19
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,456
Cool

Try this:
Code:
#!/bin/ksh
awk '{
if ( index($0,"TEST123") > 0 ) flg=1;
if ( flg == 1 ) {print $0;
if ( index($0,";") > 0 ) {print ""; flg=0;}}
}' InFile.txt >OutFile.txt
__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
  #3 (permalink)  
Old 11-23-04, 16:36
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
or a bit terser:

nawk -v str='TEST123' -f gomes.awk InFile.txt

Code:
BEGIN {
  RS=FS=""
}

($1 ~ str) { print $0, "\n" }
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+
Reply With Quote
  #4 (permalink)  
Old 02-15-05, 12:51
gomes009 gomes009 is offline
Registered User
 
Join Date: Feb 2004
Posts: 37
It is throwing the following error

+ nawk -v str=TEST123 -f gomes.awk InFile.txt
nawk: input record `UPDATE TEST123..' too long
Reply With Quote
  #5 (permalink)  
Old 02-15-05, 13:48
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
Quote:
Originally Posted by gomes009
It is throwing the following error

+ nawk -v str=TEST123 -f gomes.awk InFile.txt
nawk: input record `UPDATE TEST123..' too long
if on Solaris, try /usr/xpg4/bin/awk instead of nawk.
or if you have gawk installed - use it instead.
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+
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