Solved

Regular Expression poser

Posted on 2006-06-15
2
265 Views
Last Modified: 2010-04-21

I've got a huge sed script that converts a text document from the data dictionary into CREATE TABLE statements to generate database tables.  :)

I'm having trouble with the last update.  I need to remove the comma from the line(s) above a delimeter.  The source looks like this:

CREATE TABLE table1 (
  column1 integer, -- comment
  column2 varchar(8), --comment
);

CREATE TABLE table2 (
  column3 integer, -- comment
  column4 integer, -- comment
  column5 date, -- comment
);

etc...

I need sed to drop the last comma in each CREATE TABLE statement.

Thanks,
Kent
0
Comment
Question by:Kent Olsen
2 Comments
 
LVL 6

Accepted Solution

by:
JJSmith earned 250 total points
ID: 16914729

Sed is a stream editor, can we reasonably expect it to edit a part of the stream based on a 'later' part of the same stream?

What you want can be achived - I would be mildly suprised if 'sed' was the right choice.

Of course you could get rid of line feed in 'sed 1', pipe to 'sed 2' that could replace ", --comment );" with " --comment);" , then a pipe to 'sed 3' could insert line feeds after any "," or ";"  ..... :- bit contrived really,  but stays within the limitation of using sed!

Cheers
JJ
0
 
LVL 45

Author Comment

by:Kent Olsen
ID: 16915198

Thanks JJ,

All it took was a shot to the head for this to come into focus.  :)  What I actually did was:


sed '{very long series of conversions}' file|sed '1d;$a);'| sed -e :a -e '$!N;s/\n)/)/;ta' -e 'P;D'|sed '/);/s/, --/ --/;s/);/\n);/g'


Thanks Again,
Kent
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question