LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Sed and regular expressions (https://www.linuxquestions.org/questions/linux-software-2/sed-and-regular-expressions-81917/)

tchernobog 08-14-2003 08:40 AM

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? :confused:

Thank you in advance for any help given.

cnjohnson 08-14-2003 09:00 AM

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? :confused:

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

tchernobog 08-14-2003 12:41 PM

Thank you very much, it works perfectly!


All times are GMT -5. The time now is 02:24 PM.