Regular Expression poser

Posted on 2006-06-15
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:

  column1 integer, -- comment
  column2 varchar(8), --comment

  column3 integer, -- comment
  column4 integer, -- comment
  column5 date, -- comment


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

Question by:Kent Olsen
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

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!

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,

Featured Post

Independent Software Vendors: 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

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
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…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

707 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