Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

substituting a newline in sed...

2 views
Skip to first unread message

Pritchie

unread,
Mar 7, 2003, 7:01:36 AM3/7/03
to
Hi all,

I am trying to do something really easy and failing terribly. I have a file
with the following

blah:\n$
blah:
deblah:\n$
deblah:
morefoo:\n$
morefoo:

when I do "sed l < myfile.tx" (where l is 'el' not one)

I've tried
sed 's/\n$/:/' < myfile.tx
which I now know is wrong, the FAQ says use
sed 's/^M$/:/' < myfile.tx
and I got "^M" using "Ctrl+V" then "Ctrl+M", however it isn't working....

I've even tried
sed '/\n//s//:/' < myfile.tx

man page says
"The escape sequence \n matches a newline character embedded in the pattern
space. You can't, however, use a literal newline character in an address or
in the substitute command"
so how do you do it? will somebody tell?

anyone any idea? I am using putty with TERM=xterm, I don't know what sed
version I using (anyone know how to find out?)

many thanks
SJP


kim

unread,
Mar 7, 2003, 11:35:12 AM3/7/03
to
Pritchie wrote:

you should look at the sed faq, it addresses this problem:

sed '{
N
s/\n//
}' myfile.tx

of course, you probably want to put a pattern in there.

Pritchie

unread,
Mar 7, 2003, 1:55:25 PM3/7/03
to
Kim Wrote
"kim" <wykoffkbS...@yahoo.com> wrote in message
news:3E68CA3F...@yahoo.com...

:o( but I did... that where I got this from

"...
# 3. Under UNIX: convert DOS newlines (CR/LF) to Unix format
sed 's/.$//' file # assumes that all lines end with CR/LF
sed 's/^M$// file # in bash/tcsh, press Ctrl-V then Ctrl-M
..."
it didn't work for me.

>
> sed '{
> N
> s/\n//
> }' myfile.tx

This semi works


file my.tx =>
my-file
your file
his-file


sed '{
N
s/\n//
}' my.tx


gives me... (with a trailing newline at the end of the file)

my-fileyour file
his-file
bash-2.04#

(with no trailing newline at the end of the file)
my-fileyour file
bash-2.04#


ummm..... :o(

what FAQ were you looking at?
I looked at
http://www.dreamwvr.com/sed-info/sed-faq.html
and this one....

From: epe...@jpusa.chi.il.us
Newsgroups:
alt.comp.editors.batch,comp.editors,alt.answers,comp.answers,news.answers
Subject: sed FAQ, version 011
Followup-To: poster
Summary: Frequently Asked Questions about sed, the stream editor
X-Disclaimer: Approval for *.answers is based on form, not content

Archive-name: editor-faq/sed
Posting-Frequency: bimonthly
Last-modified: 1999/07/18
Version: 011
URL: http://www.cornerstonemag.com/sed/sedfaq.html
Maintainer: Eric Pement <epe...@jpusa.chi.il.us>


THE SED FAQ

Frequently Asked Questions about
sed, the stream editor

>
> of course, you probably want to put a pattern in there.
>

In where?


kim

unread,
Mar 10, 2003, 4:15:54 AM3/10/03
to
Pritchie wrote:

sorry, i realize that this was explained in chapter 6.1 of the o'reilly
sed&awk book, not in the faq.

> >
> > of course, you probably want to put a pattern in there.
> >
> In where?

at the beginning
sed '/$/{
$!N
s/\n//
}'

but in your case, I don't think you need one since you want to do it for every
line. i didn't really study your needs or this sed command. i was just hoping
to point you in the right direction.

doing this with sed seems complicated. It seems like you would have to perform
the sed command several times until you end up with one line.

why not use awk? :

awk '{printf("%s", $0);} END {printf("\n")}' yourfile.dat


Pritchie

unread,
Mar 10, 2003, 9:06:13 AM3/10/03
to
Hi Kim,

I used tr in the end... eg sed 's/"//g' < myfile | tr "\n" ":"

Many thanks for trying to help, I wasn't getting at you... honest :o),
more frustrated at the fact I can't do what I consider to be a simple thing,
simply. Maybe its my lack of understanding as I am newbie, I just thought
is would be easy as must things are in the "Unix" world (once you know the
command to do it). I did consider using "awk", however I am a bigger newbie
to awk than sed. I'll look into your AWK solution. cheers

The o'reilly book, is it any good? would you recommend it?
SJP

"kim" <wykoffkbS...@yahoo.com> wrote in message

news:3E6C57C7...@yahoo.com...

John Savage

unread,
Mar 12, 2003, 3:49:08 PM3/12/03
to
"Pritchie" <nn...@pritchie.com-nospam> writes:
>I am trying to do something really easy and failing terribly. I have a file
>with the following
>
>blah:\n$
>blah:
>deblah:\n$
>deblah:
>morefoo:\n$
>morefoo:
>
>when I do "sed l < myfile.tx" (where l is 'el' not one)

That doesn't sound right to me. \n implies a newline, but that should
show up as an actual blankline, not an escape sequence. Perhaps your
version of sed does LIST a normal line ending as \n$ then? I suggest
that you post the output of this line before we proceed further:

head -2 myfile.txt | od -c

Normally you would use sed -n l myfile.tx to display the listing so
that you don't see those unwanted duplicates. For the version, try

sed -V

Postscript: I see in a later post where you reveal that you are really
wanting to convert a 4 (or more) line file to a single string by deleting
all newline chars. Had you said this from the start, then the responses
here would have been more useful. Note that sed always outputs *lines*,
and each comes with its terminating \n whether you'd like it or not. The
minimum non-null output from sed is one line.
--
John Savage (newsgroup email invalid; keep news replies in newsgroup)

0 new messages