Solved

Regular Expression poser

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

746 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

8 Experts available now in Live!

Get 1:1 Help Now