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

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

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
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…
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.

840 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