LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Matching newlines with sed, not working (https://www.linuxquestions.org/questions/programming-9/matching-newlines-with-sed-not-working-722202/)

hamtavs 04-28-2009 05:21 AM

Matching newlines with sed, not working
 
Hi folks,

I need to make a fortune plain-text file from plain-text files, i.e. substitute every <newline><newline> occurrence in plain-text files with <newline>% <newline>.

I tried to use sed as follows
Quote:

sed -e 's/^\n/\n%\n/g' myfile
but it doesn't perform any substitution, nor if I use \\n instead of \n
An I also tried using the raw newline character
Quote:

sed -e 's/\
/\n%\n/g' myfile
and
Quote:

sed -e 's/
/\n%\n/g' myfile
with no results.
\n seems to work only as substitution(2nd) argument.
I'm using gnu sed 4.1.5.

You may be able to save me the trouble of converting such files in first instance. Do you know where I can download fortunes with quotes about programming languages, software costruction, etc... found in SysProg's site?

TIA

Tavs

colucix 04-28-2009 05:35 AM

Use $ instead of \n as pattern:
Code:

sed -e 's/$/\n%\n/g' myfile

syg00 04-28-2009 05:38 AM

sed won't pass you the newline - try perl.

ghostdog74 04-28-2009 05:49 AM

show a sample of your file and your output.

hamtavs 04-28-2009 08:07 AM

Wow, so much replies! Thanks a lot!

Here's the input, got from copy&paste quotes from SysProg site

Quote:

I don’t see the logic of rejecting data just because they seem incredible. — Fred Hoyle

If you torture data sufficiently, it will confess to almost anything. — Fred Menger

Numbers do not lie, but they have the propensity to tell the truth with intent to deceive. — Eric Temple Bell

Another type of backup storage uses a number of large, thin discs (about three feet in diameter), with magnetic coating on the surfaces. — Douglas Engelbart

Software: These programs give instruction to the CPU, which processes billions of tiny facts called bytes, and within a fraction of a second it sends you an error message that requires you to call the customer-support hot line and be placed on hold for approximately the life-span of a caribou. — Dave Barry
The sed script worked with
Quote:

sed -e 's/^$/\n%\n/g'
It would be nice if I could remove extra blank lines (i.e. lines matching (^\s+\n){2}), but I haven't managed yet to get the correct expression.

PS: I'm using sed 'cause I learnt reg-expression through emacs, lisp and java, thus I'm not used to quick&easy one-liner commands, and I know the sed substitute command only, so be really patient! :D


All times are GMT -5. The time now is 05:46 PM.