LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-14-2003, 08:40 AM   #1
tchernobog
Member
 
Registered: Aug 2003
Distribution: Latest Gentoo; Mandrake 9.1
Posts: 37

Rep: Reputation: 15
Question Sed and regular expressions


Ok, it sounds a little "newbieish"...

I'm trying to create a fortune db. I have a looong text file, with every citation separated by a blank line.

Then, my problem is to use sed to substitute every blank line in the file with a percent symbol.

I tried in the following way :

sed 's/\n/%/gi' ~/source

(redirecting it to "~/dest" to obtain the db).

I've tried other regexp, but no-one did work.
So, the question is, what's the right one?

Thank you in advance for any help given.
 
Old 08-14-2003, 09:00 AM   #2
cnjohnson
Member
 
Registered: Nov 2002
Location: Nashville
Distribution: FreeBSD, Linux, OS-X
Posts: 544

Rep: Reputation: 30
Re: Sed and regular expressions

Quote:
Originally posted by tchernobog
Ok, it sounds a little "newbieish"...

I'm trying to create a fortune db. I have a looong text file, with every citation separated by a blank line.

Then, my problem is to use sed to substitute every blank line in the file with a percent symbol.

I tried in the following way :

sed 's/\n/%/gi' ~/source

(redirecting it to "~/dest" to obtain the db).

I've tried other regexp, but no-one did work.
So, the question is, what's the right one?

Thank you in advance for any help given.
sed is line oriented, so it uses the newlines \n as delimiters. So, you have to learn about the two line buffer to deal with them.

Fortunately, you don't have to deal with them, since you really don't want to replace the newline, you just want to add a percent sign on every blank line. The regex for a blank line is

/^$/

i.e., the beginning of the line followed immediately by the ending of the line. So you want this:

s/^$/%/

as your regex.

Cheers--
Charles
 
Old 08-14-2003, 12:41 PM   #3
tchernobog
Member
 
Registered: Aug 2003
Distribution: Latest Gentoo; Mandrake 9.1
Posts: 37

Original Poster
Rep: Reputation: 15
Thank you very much, it works perfectly!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular Expressions markjuggles Programming 2 05-05-2005 11:39 AM
Regular Expressions overbored Linux - Software 3 06-24-2004 02:34 PM
help with REGULAR EXPRESSIONS ner Linux - General 23 10-31-2003 11:09 PM
Regular expressions aromes Linux - General 1 10-15-2003 12:29 PM
regular expressions? alaios Linux - General 2 06-11-2003 03:51 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration