regular expression across some lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting regular expression across some lines
# 1  
Old 05-24-2007
regular expression across some lines

I am trying to use regular expression to identify ONLY the commands that hasn't the word "tablespace" within it. a command starts with "create table" and ends with ; (semicolon)

example file:
Code:
create table first tablespace ;
create table second
(
  BBL_CUSTOMER_NAME      VARCHAR2(32),
a tablespace af
);
create table third
(
  BBL_CUSTOMER_NAME      VARCHAR2(32),
);
create table 
forth
(
  BBL_CUSTOMER_NAME      VARCHAR2(32) );

the match should be:
Code:
create table third
(
  BBL_CUSTOMER_NAME      VARCHAR2(32),
);
create table 
forth
(
  BBL_CUSTOMER_NAME      VARCHAR2(32) );

HELP ME

Last edited by ynixon; 05-24-2007 at 12:02 PM..
# 2  
Old 05-24-2007
You can try something like that :
Code:
awk '
   BEGIN { 
      RS=ORS=";\n"
   }
   /create/ && ! /tablespace/ {
      sub(/^\n*/, "");
      print
   }
    ' inputfile

Jean-Pierre.
# 3  
Old 05-24-2007
Code:
awk -v RS=";\n" -v ORS=";\n" ' !/tablespace/ ' filename

# 4  
Old 05-24-2007
I'd like to try using only grep
# 5  
Old 05-24-2007
Hi.

I think that grep will not cross newlines, so that you would need to place your commands each on a single line. There is a note in my man grep page about using perl regular expressions, but that it is undocumented. The awk and perl scripts can make use of settable record boundaries.

Here's a perl quickie:
Code:
#!/bin/sh

# @(#) s1       Demonstrate perl quickie for unmatched string across lines.

FILE=${1-data1}

perl -wn -e 'BEGIN{$/=";"} print if not /tablespace/' $FILE

exit 0

And running this on your sample contained in file data1:
Code:
% ./s1

create table third
(
  BBL_CUSTOMER_NAME      VARCHAR2(32),
);
create table
forth
(
  BBL_CUSTOMER_NAME      VARCHAR2(32) );

Best wishes ... cheers, drl
# 6  
Old 05-24-2007
Quote:
Originally Posted by ynixon
I'd like to try using only grep
why is that?
# 7  
Old 05-24-2007
Quote:
Originally Posted by vgersh99
why is that?
unfortunately the final solution will be used in windows environment Smilie ,
I am not planning to use Perl because it will need installations in all PCs.
the nuances of regular expression is almost the same in all other languages.
So the convenient solution will be vbscript.

Since I know you are ACEs I decided to ask you Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regular expression to match multiple lines?

Using a regular expression, I would like multiple lines to be matched. By default, a period (.) matches any character except newline. However, (?s) and /s modifiers are supposed to force . to accept a newline and to match any character including a newline. However, the following two perl... (4 Replies)
Discussion started by: LessNux
4 Replies

2. Shell Programming and Scripting

regular expression grouping across multiple lines

cat book.txt book1 price 23 sku 1234 auth Bill book2 sku 1233 price 22 auth John book3 auth Frank price 24 book4 price 25 sku 129 auth Tod import re f = open('book.txt', 'r') text = f.read() f.close() m =... (2 Replies)
Discussion started by: chirish
2 Replies

3. UNIX for Advanced & Expert Users

sed: -e expression #1, char 0: no previous regular expression

Hello All, I'm trying to extract the lines between two consecutive elements of an array from a file. My array looks like: problem_arr=(PRS111 PRS213 PRS234) j=0 while } ] do k=`expr $j + 1` sed -n "/${problem_arr}/,/${problem_arr}/p" problemid.txt ---some operation goes... (11 Replies)
Discussion started by: InduInduIndu
11 Replies

4. UNIX for Dummies Questions & Answers

Finding lines with a regular expression, replacing them with blank lines

So the tag for this forum says all newbies welcome... All I want to do is go through my file and find lines which contain a given string of characters then replace these with a blank line. I really tried to find a simple command to do this but failed. Here's what I did come up with though: ... (2 Replies)
Discussion started by: Golpette
2 Replies

5. UNIX for Dummies Questions & Answers

delete lines matching a regular expression

I have a very large file (over 700 million lines) that has some lines that I need to delete. An example of 5 lines of the file: HS4_80:8:2303:19153:193032 153 k80:138891 HS4_80:8:2105:5544:43174 89 k88:81949 165 k88:81949 323 0 * = 323 0 ... (6 Replies)
Discussion started by: pathunkathunk
6 Replies

6. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

7. Shell Programming and Scripting

Would like to print 3 lines after a regular expression is found in the logfile

I would like to print 3 lines after a regular expression is found in the logfile. I'm using the following code: grep -n "$reg_exp" file.txt |while read LINE ;do i=$(echo $LINE |cut -d':' -f1 ) ;sed -n "$i,$(($i+3))p" file.txt ;done The above code things works fine,but sometimes gives erroneous... (3 Replies)
Discussion started by: joachimshaun
3 Replies

8. Shell Programming and Scripting

Integer expression expected: with regular expression

CA_RELEASE has a value of 6. I need to check if that this is a numeric value. if not error. source $CA_VERSION_DATA if * ] then echo "CA_RELESE $CA_RELEASE is invalid" exit -1 fi + source /etc/ncgl/ca_version_data ++ CA_PRODUCT_ID=samxts ++ CA_RELEASE=6 ++ CA_WEEK_NO=7 ++... (3 Replies)
Discussion started by: ketkee1985
3 Replies

9. Shell Programming and Scripting

sed not printing lines before a regular expression.

Hey, I found a way to print the lines which is just before a regular expression, not including the expression. sed -n '/regexp/{n;p;}' myfile Now I'm looking for a way to print all lines, exept the regular expression and also the line before the same regular expression. Use code tags. (1 Reply)
Discussion started by: Livio
1 Replies

10. Shell Programming and Scripting

regular expression grepping lines with VARIOUS number of blanks

Hi, I need a regular expression grepping all lines starting with '*' followed by a VARIOUS number of blanks and then followed by the string 'Runjob=1'. I tried that code, but it doesn't work: grep -i '*'+'Runjob=1' INPUT_FILE >>OUTPUT_FILE Can someone help me? Thanks (8 Replies)
Discussion started by: ABE2202
8 Replies
Login or Register to Ask a Question