sed replacement with newline

sed replacement with newline

Post by David SAMSOE » Thu, 19 Jun 2003 01:59:21



I'm not sure the subject is very clear, so I'll explain

I know how to replace a regexp with some text using
prompt> sed -e 's/foo/bar/g'

My problem arises when the replacement tag (bar in the
example above) should contains newline

Reading the sed manual I have tried the following script
which, I thought, would replace the word foo in infile by
bar
junk
in outfile

sed -e 's/foo/bar\
junk/g' infile > outfile

Which doesn't work:
sed: -e expression #1, char9: Unterminated `s' command

Can somebody help me ?
(The problem is most propably due to my difficulties in
translating the man page into my native french language)

Thanks.
__
Dr. David SAMSOEN                        | CENBG, Le Haut Vigneau, BP 120
Phone/Fax: (33) [0] 557-120  868/801     | Groupe de Physique Thorique
"Ces vnements nous dpassent, feignons | 33175 Gradignan Cedex - France
 d'en tre les organisateurs"  J. Cocteau

 
 
 

sed replacement with newline

Post by Antoine Mathy » Thu, 19 Jun 2003 06:56:01


Hello,

Quote:> ... I have tried the following script
> which, I thought, would replace the word foo in infile by
> bar
> junk
> in outfile

> sed -e 's/foo/bar\
> junk/g' infile > outfile

Use :
        sed -e 's/foo/bar\njunk/g' infile > outfile

'\n' is a character constant which stands for newline.

A.M.

 
 
 

sed replacement with newline

Post by David SAMSOE » Thu, 19 Jun 2003 17:09:05



> Hello,

> > ... I have tried the following script
> > which, I thought, would replace the word foo in infile by
> > bar
> > junk
> > in outfile

> > sed -e 's/foo/bar\
> > junk/g' infile > outfile

> Use :
>    sed -e 's/foo/bar\njunk/g' infile > outfile

> '\n' is a character constant which stands for newline.

This command replaces 'foo' by 'barnjunk' ...
I use GNU sed version 3.02
__
Dr. David SAMSOEN                        | CENBG, Le Haut Vigneau, BP 120
Phone/Fax: (33) [0] 557-120  868/801     | Groupe de Physique Thorique
"Ces vnements nous dpassent, feignons | 33175 Gradignan Cedex - France
 d'en tre les organisateurs"  J. Cocteau
 
 
 

sed replacement with newline

Post by Antoine Mathy » Sun, 22 Jun 2003 04:09:28


Quote:>>        sed -e 's/foo/bar\njunk/g' infile > outfile

> This command replaces 'foo' by 'barnjunk' ...
> I use GNU sed version 3.02

It works fine with version 4.07
Can't help you out.

A.M.

 
 
 

sed replacement with newline

Post by Yu C » Sun, 22 Jun 2003 06:21:31



>>>    sed -e 's/foo/bar\njunk/g' infile > outfile

>> This command replaces 'foo' by 'barnjunk' ...
>> I use GNU sed version 3.02
>It works fine with version 4.07
>Can't help you out.

With some old version of sed a workaround is to put the sed command in
a script file (containing an escaped new line and not \n), and use the -f
flag instead of -e. Not sure if that's good enough for the OP.

--Yu

 
 
 

sed replacement with newline

Post by Stephane CHAZELA » Tue, 24 Jun 2003 06:51:26


[...]
Quote:> Reading the sed manual I have tried the following script
> which, I thought, would replace the word foo in infile by
> bar
> junk
> in outfile

> sed -e 's/foo/bar\
> junk/g' infile > outfile

> Which doesn't work:
> sed: -e expression #1, char9: Unterminated `s' command

[...]

You must be using csh or tcsh.

With those shells, you've got to do:

sed -e 's/foo/bar\\
junk/g' infile > outfile

But the best you can do is to give up those shells.

--
Stphane

 
 
 

sed replacement with newline

Post by Cliff Fu Xuan Che » Thu, 03 Jul 2003 22:27:29


Hi all,

Does someone can help me on Stream programming to give some stream links or
example program because I'm real a green hand.

Thanks
Cliff

 
 
 

sed replacement with newline

Post by Fritz » Fri, 04 Jul 2003 01:48:24



Quote:> Does someone can help me on Stream programming to give some stream
> links or example program because I'm real a green hand.

The online Streams programming guides all have examples.  I'm sure several
of your co-workers at Lucent could also give you a hand; one of them is the
inventor of Streams.

RFM
--
To reply, translate domain from l33+ 2p33|< to alpha.
                4=a  0=o  3=e  +=t

 
 
 

sed replacement with newline

Post by Michael Bruschkewit » Sat, 05 Jul 2003 20:19:03



Quote:> Hi all,

> Does someone can help me on Stream programming to give some stream links or
> example program because I'm real a green hand.

> Thanks
> Cliff

google: streams programming [ENTER]
... 3 sec ...
http://docs.sun.com/db/doc/806-6546
 
 
 

sed replacement with newline

Post by Cliff Fu Xuan Che » Sat, 05 Jul 2003 22:34:54


Hi RFM,

Thank you very much.

Do you know the name in lucent  who know the stream?

Thanks
Best regards,
Cliff



> > Does someone can help me on Stream programming to give some stream
> > links or example program because I'm real a green hand.

> The online Streams programming guides all have examples.  I'm sure several
> of your co-workers at Lucent could also give you a hand; one of them is the
> inventor of Streams.

> RFM
> --
> To reply, translate domain from l33+ 2p33|< to alpha.
>                 4=a  0=o  3=e  +=t

 
 
 

sed replacement with newline

Post by Cliff Fu Xuan Che » Sat, 05 Jul 2003 22:33:55


Hi RFM,

Thank you very much.

Do you know the name in lucent  who know the stream?

Thanks
Best regards,
Cliff



> > Does someone can help me on Stream programming to give some stream
> > links or example program because I'm real a green hand.

> The online Streams programming guides all have examples.  I'm sure several
> of your co-workers at Lucent could also give you a hand; one of them is the
> inventor of Streams.

> RFM
> --
> To reply, translate domain from l33+ 2p33|< to alpha.
>                 4=a  0=o  3=e  +=t

 
 
 

sed replacement with newline

Post by Cliff Fu Xuan Che » Sat, 05 Jul 2003 22:33:47


Hi RFM,

Thank you very much.

Do you know the name in lucent  who know the stream?

Thanks
Best regards,
Cliff



> > Does someone can help me on Stream programming to give some stream
> > links or example program because I'm real a green hand.

> The online Streams programming guides all have examples.  I'm sure several
> of your co-workers at Lucent could also give you a hand; one of them is the
> inventor of Streams.

> RFM
> --
> To reply, translate domain from l33+ 2p33|< to alpha.
>                 4=a  0=o  3=e  +=t

 
 
 

sed replacement with newline

Post by Andrew Gabri » Sat, 05 Jul 2003 23:10:11




Quote:

> Hi RFM,

>> The online Streams programming guides all have examples.  I'm sure several
>> of your co-workers at Lucent could also give you a hand; one of them is the
>> inventor of Streams.

> Thank you very much.

> Do you know the name in lucent  who know the stream?

Dennis Ritchie

--
Andrew Gabriel
Consultant Software Engineer

 
 
 

sed replacement with newline

Post by Fritz » Thu, 10 Jul 2003 00:17:03



Quote:> Do you know the name in lucent  who know the stream?

I was being a little bit facetious in my answer, but the inventor of
Streams is a guy by the name of Dennis Ritchie.  He's pretty obscure so you
might not have heard of him.  He works at the Computer Sciences Research
Center at Bell Labs in New Jersey.

Seriously though, look at the online Streams reference guides.  Are you
interested in writing kernel modules or user programs?

RFM
--
To reply, translate domain from l33+ 2p33|< to alpha.
                4=a  0=o  3=e  +=t

 
 
 

1. newline sed replacement

how can I replace in file which has many occurances of

blah blah blah foo
etc
etc
blah blah blah foo
etc
etc
[and so on]

the foo<newline> with just foo?
The resulting output should be

blah blah blah foo etc
etc
blah blah blah foo etc
etc
[and so on]

I have tried the following

sed 's/foo\n/foo/'
sed 's/foo\\n/foo/'
sed 's/foo<^M inserted via ^V^M>/foo/'

The man page for sed says:

...which is a  /regular  expression/
          in the style of ed(1);

     with the following exceptions:

\n   Matches a NEWLINE embedded in the pattern space

But the \n did not work.  Any help is appreciated.

Thanks,
Brian

2. ISA network card.

3. How to add "newline" to a replacement string in sed or awk?

4. Strange characters in left column printing with an OL400e on /dev/ttyS0

5. sed substituution with a newline in replacement text

6. Standard C++ library on solaris

7. another sed problem: sed and newline chars

8. looking for info........

9. summery newline to newline newline

10. Help with replacement involving newline

11. substituting a newline in sed...

12. sed to remove newlines?

13. sed newline question