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:Kdo

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sudo logging 5 80
Compare if date string is within text file 3 60
Unix Question 19 45
How to clone solaris 10 machine 33 114
A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
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…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.

896 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now