LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-28-2009, 05:21 AM   #1
hamtavs
Member
 
Registered: Aug 2004
Location: Italy,near Milan
Distribution: red hat 9
Posts: 63

Rep: Reputation: 15
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
 
Old 04-28-2009, 05:35 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Use $ instead of \n as pattern:
Code:
sed -e 's/$/\n%\n/g' myfile
 
Old 04-28-2009, 05:38 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 16,976

Rep: Reputation: 2564Reputation: 2564Reputation: 2564Reputation: 2564Reputation: 2564Reputation: 2564Reputation: 2564Reputation: 2564Reputation: 2564Reputation: 2564Reputation: 2564
sed won't pass you the newline - try perl.
 
Old 04-28-2009, 05:49 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
show a sample of your file and your output.
 
Old 04-28-2009, 08:07 AM   #5
hamtavs
Member
 
Registered: Aug 2004
Location: Italy,near Milan
Distribution: red hat 9
Posts: 63

Original Poster
Rep: Reputation: 15
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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
sed: pattern matching with newlines anjanesh Linux - General 2 02-20-2009 06:36 AM
can't get non-greedy matching to work in sed lumix Linux - Newbie 3 05-05-2008 07:12 PM
SED script almosts works but with too many newlines.. cannontrodder Linux - General 2 01-16-2008 08:00 AM
Sed pattern matching digitalbrutus Programming 1 08-20-2006 01:37 PM
sed matching an apostrophe dazdaz Linux - General 2 04-28-2006 05:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:39 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration