Operating System - HP-UX
1754270 Members
2988 Online
108813 Solutions
New Discussion юеВ

Re: replacing /n with newline with ed.

 
Jonathan Corbeill
Occasional Advisor

replacing /n with newline with ed.

I am trying to replace a string in a file "/n" with a carriage return. the man page on ed uses the following example:
cat - << EOF | ed -s file-1
1,$ s/abc/xyz/
w file-2
q
EOF

how do i specify that i want to replace with a newline?

I've tried
cat - << EOF | ed -s file-1
1,$ s/\n/\r/g
w file-2
q
EOF

but that replaces the \n with the \r.
9 REPLIES 9
Steve Steel
Honored Contributor

Re: replacing /n with newline with ed.

Hi


In ed or vi

type ControlVEnter which will show ^M

This is a new line.


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Steve Steel
Honored Contributor

Re: replacing /n with newline with ed.

Hi


By enter I mean the return key


Or type ControlVControlM


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Rui Soares
Valued Contributor

Re: replacing /n with newline with ed.

Hi Jonathan,

Under the context of the ed command you're trying you'll need to "escape" the slash just before the "r" that represents the carriage return.

So use double slash:

\and press return to change line (then you just finnish the line).

Instead of:

\n

Good Luck,

Rumagoso
Be Well
Rui Soares
Valued Contributor

Re: replacing /n with newline with ed.

Oops,

my post was victim of escaping!!!!
You should have seen:
\\that is double-slash instead of \\and

Be Well
Jonathan Corbeill
Occasional Advisor

Re: replacing /n with newline with ed.

Great input guys,
I'm almost there...

My current command looks like this:

cat - << EOF | ed -s work.txt
1,$ s/n/^M/g
w work2.txt
q
EOF

all occurences of the letter n are replaced with carriage returns. Thats mostly what I want but the backslash stays in the file. How do I say that i want the backslash and the n to be replaced?
Ceesjan van Hattum
Esteemed Contributor

Re: replacing /n with newline with ed.

Instead of using editor commands, you can also use a oneliner script:

awk 'BEGIN{FS="/n"}{
for (i=1;i<=NF;i++){
printf "%s\n",$i^J }
printf "\n";
}' infile > uitfile

FS is the fieldseperator to be replaced by 'n.
I tried it for multiple lines and works just fine.

Regards,
Ceesjan
Darrell Allen
Honored Contributor

Re: replacing /n with newline with ed.

How about a sed version:

sed 's,/n,,g' file-1 >file-2

This can be entered from command line or in a script. Note it really is 2 lines (the first ending with "\").

Darrell

"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Darrell Allen
Honored Contributor

Re: replacing /n with newline with ed.

Okay, it didn't post correctly. Let's try again.

sed 's,/n,
,g' file-1 >file-2

Darrell

"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Darrell Allen
Honored Contributor

Re: replacing /n with newline with ed.

It still didn't post correctly. There is supposed to be a "\" at the end of the first line.

Let's try it as an attachment.

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)